Paul King commented on GROOVY-7237:

I'd be inclined to provide a Closure to {{asImmutable()}} which dictated how to 
make an element immutable, e.g.:
def mutable = [[1, 2], [3, 4]]
def immutable = mutable.asImmutable{ it instanceof List ? it.asImmutable() : it 
assert immutable.getClass().name.contains('Unmodifiable')
assert immutable[0].getClass().name.contains('Unmodifiable')
It might support one or two arguments to provide a hook to the closure:
def immutable = mutable.asImmutable{ val, c -> val instanceof List ? 
val.asImmutable(c) : val }

> Implement an alternative asImmutable which guards the elements of a 
> collection as well
> --------------------------------------------------------------------------------------
>                 Key: GROOVY-7237
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7237
>             Project: Groovy
>          Issue Type: New Feature
>          Components: groovy-jdk
>            Reporter: Leif Hanack
>            Priority: Major
> The current implementation of {{asImmutable}} returns a simple 
> {{Collections.unmodifiable...}}. This only guarantees that no element is 
> added or removed. Nevertheless an element itself can be modified. What do you 
> think about a feature enhancement of {{asImmutable}} where you can guard the 
> elements of a collection as well? S.th. like {{asDeepImmutable}} could proxy 
> the collection and guards all setter of all elements. 
> What do you think about it?
> Regards, Leif

This message was sent by Atlassian JIRA

Reply via email to