You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by OC <oc...@ocs.cz> on 2016/07/23 12:52:39 UTC

getAt inconsistence

Hello there,

is this the assumed behaviour? As always I might be missing something of importance, but I would expect, when overriding getAt, that it simply gets called with the appropriate argument for "foo[bar]" expressions.

Instead, alas, for strings and gstrings, getAt is not called at all. I have found getProperty is called instead, which seems wrong: far as I understand the documentation (and, well, also simply by common sense) I should get getProperty for "me.foo", but getAt for "me['foo']", should I not?

===
43 /tmp> groovy -version
Groovy Version: 2.4.7 JVM: 1.7.0_13 Vendor: Oracle Corporation OS: Mac OS X
44 /tmp> <q.groovy      
class q {
  static main(args) {
    ExpandoMetaClass.enableGlobally()
    def mmc=getMetaClass()
    mmc.getAt={ index ->
      println "getAt $index"
    }
    mmc.getProperty={ name ->
      println "getProperty $name"
    }
    def me=newInstance()
    [1,3.14,'hi',null,q,"${->'gstring'}"].each { me[it] }
  }
}
45 /tmp> groovy q       
getAt 1
getAt 3.14
getProperty hi
getAt null
getAt class q
getProperty gstring
46 /tmp> 
===

Thanks and all the best,
OC


Re: getAt inconsistence

Posted by OC <oc...@ocs.cz>.
Jochen,

On 25. 7. 2016, at 21:53, Jochen Theodorou <bl...@gmx.org> wrote:

> ... ... ... We have a method in DGM:
> 
>> public static Object getAt(Object self, String property) {
>>  return InvokerHelper.getProperty(self, property);
>> }
> 
> meaning Object itself provides a getAt method that uses getProperty - well, actually it is equal to self.$property. Which of course means if you add a getAt(Object) method and call this with a String or GString, we still call the above method. Which also explains your problem ;)

Ah, I see. Looks like a pair

===
   mmc.getAt={ index ->
     ...
   }
   mmc.getAt={ String index ->
     ...
   }
===

does the job all right; thanks a big lot again!

All the best,
OC

Re: getAt inconsistence

Posted by Jochen Theodorou <bl...@gmx.org>.
On 23.07.2016 14:52, OC wrote:
> Hello there,
>
> is this the assumed behaviour? As always I might be missing something of importance, but I would expect, when overriding getAt, that it simply gets called with the appropriate argument for "foo[bar]" expressions.
> Instead, alas, for strings and gstrings, getAt is not called at all. I have found getProperty is called instead, which seems wrong: far as I understand the documentation (and, well, also simply by common sense) I should get getProperty for "me.foo", but getAt for "me['foo']", should I not?

We have a method in DGM:

> public static Object getAt(Object self, String property) {
>   return InvokerHelper.getProperty(self, property);
> }

meaning Object itself provides a getAt method that uses getProperty - 
well, actually it is equal to self.$property. Which of course means if 
you add a getAt(Object) method and call this with a String or GString, 
we still call the above method. Which also explains your problem ;)

bye Jochen