You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2018/03/06 12:56:00 UTC

[jira] [Commented] (GROOVY-7237) Implement an alternative asImmutable which guards the elements of a collection as well

    [ https://issues.apache.org/jira/browse/GROOVY-7237?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16387709#comment-16387709 ] 

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.:
{code}
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')
{code}
It might support one or two arguments to provide a hook to the closure:
{code}
def immutable = mutable.asImmutable{ val, c -> val instanceof List ? val.asImmutable(c) : val }
{code}

> 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
(v7.6.3#76005)