4

In Groovy I can make objects immutable by doing:

@Immutable 
class MyObject {
   ...
}

But, if MyObject has a reference to a mutable object as in

@Immutable 
class MyObject {
    MutableObject mutableObject
}

I can't. I get:

classes only support properties with effectively immutable types including

But, even thou myObject has a MutableObject, the reference to it will never change.

Is there anything I can do to make MyObject as immutable as possible?

1 Answer 1

5

If you are sure that MutableObject is effectively immutable you can do

@Immutable( knownImmutableClasses=[ MutableObject ] )
class MyObject {
    MutableObject mutableObject
}

Obviously care must be taken if this is a lie and mutableObject mutates ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.