groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jochen Theodorou <blackd...@gmx.org>
Subject Re: suggestion: ImplicitSafeNavigation annotation
Date Wed, 15 Aug 2018 08:13:47 GMT


Am 14.08.2018 um 19:10 schrieb ocs@ocs:
> Jochen,
> 
>> On 14 Aug 2018, at 6:34 PM, Jochen Theodorou <blackdrag@gmx.org 
>> <mailto:blackdrag@gmx.org>> wrote:
[...]
>> are you saying x?.foo will NPE if x is null? Or that x?.getFoo() will 
>> NPE in that case? Not sure how to read your comment.
> 
> Provided only (a) “the Null.metaclass; returning null from invokeMethod” 
> is used and no ASTT, then yes, it would NPE. Easiest thing on earth to try:
> 
> ===
> 262 */tmp>* <q.groovy
> class q {
>      static main(args) {
>          // ExpandoMetaClass.enableGlobally() // I thought this is 
> needed; seems not (though my application use it anyway)
>          def mc=new OCSNMC(org.codehaus.groovy.runtime.NullObject)
>          mc.initialize()
>          org.codehaus.groovy.runtime.NullObject.metaClass=mc
> 
>          println "null.foo() is OK: ${null.foo()==null}"
>          println "null.foo we won't see: ${null.foo==null}"
>      }
> }
> 
> class OCSNMC extends DelegatingMetaClass {
>      OCSNMC(Class clazz){
>          super(clazz)
>      }
>      Object invokeMethod(Object object, String methodName, Object[] 
> arguments) {
>          null
>      }
> }

null.foo will of course NPE, if you only take care of invokeMethod. have 
to do the same treatment for get/setProperty, potentially also for 
get/setAttribute


[...]
>>> For all I know, this probably would not work properly 
>>> with @CompileStatic (which I do not use at all myself, but others do 
>>> frequently).
>>
>> the result type could be a problem... Worth to check.
> 
> Definitely :)

judging by this small snippet:

> @groovy.transform.CompileStatic
> def m() {
>   def foo = "s"
>   def bar = foo?.toUpperCase()
>   println bar.toUpperCase()
> }

and that it compiles (and runs fine) I would say the correct type is 
preserved.

>>> Trust me, been there, done that. I am pretty darn sure it would be 
>>> /infinitely/ easier and, what's important, more reliable in the core 
>>> with an explicit compiler support.
>>
>> How about making a small github project to dump the current state there?
> 
> Not that easy, for my code is mixed up with other ASTT and runtime 
> stuff; but I'll try to make some simple proof-of-concept ASAP and send here.

would help a lot

bye Jochen

Mime
View raw message