Jochen,

On 3. 4. 2016, at 7:23, Jochen Theodorou <blackd...@gmx.org> wrote:

> On 01.04.2016 03:48, OC wrote:
>> playing with possibilities of the i/i pattern, I have found one can install 
>> a static property to an interface, and then use the property all right -- 
>> see a proof-of-concept below.
>> 
>> Since it might be used e.g. to create instances or to get a factory through 
>> public API based on interfaces (which would otherwise not be possible 
>> without exposing the implementation class), this is truly interesting.
>> 
>> The question is: can I rely on this rather arcane behaviour, that it will 
>> not be broken in future Groovy versions?
> 
> if you want to be really sure it will continue working it is best if you 
> contribute a test case ;) But I see no plans to change that, especially with 
> java8 being able to define static methods and thus static properties.

Thanks a lot!

> [...]
>>       def body={-> "<$name in $delegate>" }
>>       delegate.metaClass.static."$getter"=body
>>       delegate."$getter"() // I wonder why just "body()" does not work?!?
> 
> no idea why not... but you can always try body.call() instead.

Same as with body():

===
6 /tmp> <q.groovy 
class q {
 static main(av) {
   ExpandoMetaClass.enableGlobally()
   Root.metaClass.static.propertyMissing={ name ->
     String getter="get${name.capitalize()}"
     def body={-> "<$name in $delegate>" }
     delegate.metaClass.static."$getter"=body
     body.call() // same problem with body(); delegate."$getter"() would work 
though
   }
   println "- ${Root.weird}"
 }
}
interface Root {}
7 /tmp> groovy q.groovy
Caught: groovy.lang.MissingMethodException: No signature of method: 
java.io.StringWriter.capitalize() is applicable for argument types: () values: 
[]
groovy.lang.MissingMethodException: No signature of method: 
java.io.StringWriter.capitalize() is applicable for argument types: () values: 
[]
        at q$_main_closure1.doCall(q.groovy:5)
        at q.main(q.groovy:10)
8 /tmp> 
===

Thanks again and all the best,
OC

Reply via email to