You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Douglas Paul (Jira)" <ji...@apache.org> on 2021/05/25 16:18:00 UTC
[jira] [Commented] (GROOVY-10108) Tuple constructor fails for an
@Immutable class having a single Map property
[ https://issues.apache.org/jira/browse/GROOVY-10108?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17351175#comment-17351175 ]
Douglas Paul commented on GROOVY-10108:
---------------------------------------
By the way, I'm aware this case is inherently tricky to handle. There can of course only be one constructor that takes a single {{Map}} parameter, and within that constructor it's ambiguous as to whether an invocation is intended to be map-based construction or tuple-based construction.
However, I felt it was worth filing an issue since it did work in Groovy 2.4 and I didn't see any documentation about support for this use case being intentionally dropped.
> Tuple constructor fails for an @Immutable class having a single Map property
> ----------------------------------------------------------------------------
>
> Key: GROOVY-10108
> URL: https://issues.apache.org/jira/browse/GROOVY-10108
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 2.5.0, 2.5.14, 3.0.8
> Environment: N/A
> Reporter: Douglas Paul
> Priority: Major
>
> For an {{@Immutable}} class having a single {{Map}} property, trying to use the generated tuple constructor generally results in an exception being thrown from {{ImmutableASTTransformation.checkPropNames()}}. This did not happen in Groovy 2.4.
> For example, given a class like this:
> {code:groovy}
> @Immutable
> class LookupTable {
> Map<Long, String> data
> }
> {code}
> And this usage of that class:
> {code:groovy}
> Map<Long, String> data = [(1L): 'foo', (2L): 'bar']
> new LookupTable(data)
> {code}
> The constructor invocation throws:
> {noformat}
> java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
> at org.codehaus.groovy.transform.ImmutableASTTransformation.checkPropNames(ImmutableASTTransformation.java:423)
> {noformat}
> The documentation for {{@Immutable}} does mention there are limitations when there is a single property of type {{Map}}, but the wording there doesn't sound like it's talking about the tuple constructor:
> {quote}
> Groovy's normal map-style naming conventions will not be available if the first property has type {{LinkedHashMap}} or if there is a single {{Map}}, {{AbstractMap}} or {{HashMap}} property.
> {quote}
> In fact that wording makes it sound as if it's the map-based constructor that won't be available, when in fact using the map-based constructor does work in this case (and thus seems to be a viable workaround).
> See https://github.com/douglyuckling/groovy-2.5-immutable-with-map-bug for a minimal reproduction of the problem.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)