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

Reply via email to