Jochen, Thank you for your quick response.
I did consider this, but I want to be able to use a mutable where-ever immutable is allowed. For example, if a function can take all immutable parameters, I want to be able to call it with mutable types as well. So my choices are either bypass typing when I try to use mutable where immutables are needed (in addition to dealing with the generics issue I called out) or just deal with the generics issue I am looking to solve. Either way, I'll have to do something (or am I thinking too much :D) I specifically want this to work, because my DSL is trying to implement ReadOnly constructs so Mutable/Immutable are not really types seen by the programmer, its just used during the type checking mechanism to make sure something marked @ReadOnly is not modified So the programmer will type void fn1(@ReadOnly Entity myInputParameter) { List<Entity> things = [] things << myInputParameter // This should cause a compile error myInputParameter.myProperty = "1234 } and I want to see void fn1(Entity.Immutable myInputParameter) { // I want this to work but it wont because of groovy type checking List<Entity.Mutable> things = [] things << myInputParameter // This will automatically fail (like I want it to) because setMyProperty is not a method in immutable and is only available in mutable myInputParameter.myProperty = "1234" } // Now both these will compile fine fn1(new Entity.Mutable()) fn1(new Entity.Immutable()) It does look like I am bypassing the type system, but only in certain cases specifically generics regards Saravanan On Sun, Aug 27, 2023 at 6:58 PM Jochen Theodorou <blackd...@gmx.org> wrote: > hi, > > Basically you want to use an AST transform to bypass the type system? I > don't have enough data, but I wonder if you cannot solve the problem > without this. > > I mean... why does Mutable extend Immutable? How about... > > interface Entity {} > class Mutable implement Entity [} > class Immutable implements Entity {} > > if you use sealed, then those two classes can be designated as the only > implementations as well. Though I assume that is not what you intend. > > Then you should be able to do /I have tested nothing): > > List<? extends Entity> myOtherThings = [] > myOtherThings << new Immutable() > myOtherThings << new Mutable() > > List<Immutale> myThings = [] > myThings << new Immutable() > // myThings << new Mutable() forbidden > > List<Mutable> myMutables = [] > // myMutables << new Immutable() forbiddden > myMutables << new Mutable() > > On 27.08.23 14:59, Saravanan Palanichamy wrote: > > Hello folks > > > > I am building an AST transformation that will allow me to define a > > concept of mutable and immutable objects. Java lets me hold mutable > > objects in immutable object lists (as per the code below). But my DSL > > also wants to allow immutable objects to be held in mutable lists. The > > meaning it is conveying is that adding to mutable lists will clone the > > immutable object and not affect the original ( I use the << operator to > > denote this. It denotes that the object is being streamed into the list > > and will not affect the object if list contents are modified) . What is > > the best way to achieve this? I dont see a type checking extension that > > will catch this missing "method" and let me override it. > > > > I dont want to create an extension function, because this could be a > > model I want to follow for several classes and not just Blah. Which is > > why I want to use the AST route to solve this problem. Any pointers will > > be very helpful. > > > > Assume that I can convert immutable to mutable behind the scenes. Also > > assume that 100s of classes will follow this model of mutability > > > > static class Blah { > > static class Immutable { > > String getProperty1() { "PROP1" } > > } > > static class Mutable extends Immutable { > > void setProperty1(String value) { > > } > > } > > } > > void fn() { > > // This works > > List<Blah.Immutable> myOtherThings = [] > > myOtherThings << new Blah.Immutable() > > myOtherThings << new Blah.Mutable() > > > > // This does not (I want an AST that will make this work) > > List<Blah.Mutable> myThings = [] > > myThings << new Blah.Immutable() > > > > // This works > > myThings << new Blah.Mutable() > > } > > > > > > regards > > Saravanan > >