I believe the feature at play is "flow typing", and it surprises me that it would apply to variables declared with an explicit type. What version of Groovy are you targeting, and is this compiled statically or dynamically?
On Sat, Dec 3, 2022, 05:04 Saravanan Palanichamy <chava...@gmail.com> wrote: > Hello developers > > I am developing a DSL and ran into this issue. Groovy auto infers that b > is really a derived clazz and allows me to call the derived clazz method. > How do I prevent this behavior from happening? Are there some settings in > the DeclarationExpression or VariableExpression AST models that will allow > me to prevent this and cause a compile error? (I am writing a compiler > customizer and so have access to do this) > > void myMethod() { >> BaseClazz b = new DerivedClazz() >> b.callMethodDefinedOnlyInDerivedClazz("") >> } >> >> > Alternately, this seems to work correctly. But I'd rather not do it this > way if possible > > void myMethod() { >> BaseClazz b = (BaseClazz) new DerivedClazz() >> b.callMethodDefinedOnlyInDerivedClazz("") >> } >> >> regards > Saravanan >