Hi OC,
I had to check (since I don't use code like that), and it is not
overriding/overloading the new operator per se, but replacing a ctor of
a class through its metaClass (see code below).
Cheers,
mg
import groovy.transform.Canonical
@Canonical class Goo { String s }
@Canonical class FooFoo { String s }
Goo.metaClass.constructor = { String s -> new FooFoo(s) }
final goo = new Goo("abc")
println SV(goo,goo.getClass()) // goo=FooFoo(abc), goo.getClass()=class
FooFoo
println SV(new Goo("abc")) // new Goo(abc)=FooFoo(abc)
println SV(new Goo("abc").getClass()) // new Goo(abc).getClass()=class
FooFoo
On 23/11/2024 16:53, OCsite wrote:
Hi there,
On 23. 11. 2024, at 4:12, MG <mg...@arscreat.com> wrote:
Groovy new can be overridden to return a different type on purpose
can it? How? (Aside of an ASTT which would be a bit at the complex
side for such a trivial thing.) I must have missed this thing in the
documentation somewhere, and searching the documentation for “new” or
“override” does not really help for obvious reasons :)
I am using consistently /newInstance/ in my code so that I can
override it easily if need be; a possibility to use the plain ole
/new/ same way would be nice, especially when working with a legacy code.
Thanks,
OC