문제

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?

도움이 되었습니까?

해결책

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 ;-)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top