groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From MG <>
Subject Re: @CompileStatic void method returns null ?
Date Mon, 03 Sep 2018 21:37:47 GMT
What I meant was: What sense does letting void methods be called make 
for the dynamic case, i.e. the dynamic compiler ? From a programmer's 
perspective, i.e. what is a programming use case for that 
feature/behavior, in dynamic Groovy ?

Of course I can do the following in dynamic Groovy:

// Groovy 2.5.0
class Goo {
     //void nogoo() { return 123 } // Dynamic Groovy compiler: RuntimeParserException:
Cannot use return statement with an expression on a method that returns void
     void nogoo() { 123 }

final goo = new Goo()

println "original: goo.nogoo()=${goo.nogoo()}"

goo.metaClass.nogoo = { return 456 }

println "mopped: goo.nogoo()=${goo.nogoo()}"

Which will build, run, and output

original: goo.nogoo()=null
mopped: goo.nogoo()=456

  i.e. returning 456 from a void method in the second case.
But if I am using a library that includes the Goo class, why would I 
ever expect a return value from the nogoo method (and therefore call 
it), considering its return type is void ? And if I control the Goo 
class myself, why would I not just change its return type to int or def ?


On 03.09.2018 22:36, Jochen Theodorou wrote:
> On 03.09.2018 17:13, mg wrote:
>> But in what scenario does the dynamic behavior make sense ?
> for a static compiler? none other than being compatible
> bye Jochen

View raw message