Jochen, > On 26 Jun 2020, at 17:33, Jochen <blackd...@gmx.org> wrote: > > On 26.06.20 17:21, Jochen Theodorou wrote: > [...] >> public class X { >> private String foo; >> public String getFoo(){ return this.foo; } >> public void setFoo(String foo){ this.foo = foo; } >> } >> >> This works perfectly fine in Java
Correct me please if I am wrong, but the reason it work in the thing is that in Java this.foo is something completely different than in Groovy. In Java, this.foo is an access to the instance variable (it would never call getFoo, or would it?). The Groovy equivalent is this.@foo. In Groovy, contrariwise, it is an access to the property (it should never access the instance variable directly, should always go through the getter, if one exists). The Java equivalent is this.getFoo(). >> and would lead to a stack overflow in >> Groovy as soon as you call the getter or setter. Since it is quite >> common we have a problem here. > > actually I am wondering.. what if we made this a compilation error? I have considered that, but IMO, a language should not limit the programmer; it should allow him to do what he really needs (and if he's stupid enough to shoot his own leg, it's definitely not the language's job to prevent it). Compare e.g. the case of a code before super in a constructor: to forbid it is a nonsense (which, quadruple alas, Groovy inherited from the Java crap). What if there's a case when someone would want a limited recursion? def getX() { some_condition?this.x:1 } That said, an error would be infinitely better than the current behaviour :) Thanks and all the best, OC