[
https://issues.apache.org/jira/browse/GROOVY-11807?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18092205#comment-18092205
]
Paul King commented on GROOVY-11807:
------------------------------------
I'll split out the @OverrideCall idea into its own issue. An alternative is to
annotate the/each param rather than the method, e.g. @ClassTag or @Reify. I'll
try to flesh out something along those lines in the new issue and leave
supplying a workaround:
{code:groovy}
withDefault(Map<K, V> self, Class<K> keyTypeRef, Closure<V> init)
{code}
As this issue, so that at least folks could have some extra protection if they
were willing to supply the class by hand and could become automatic under
@CompileStatic at a future point in time.
> MapWithDefault can break type safety of key set
> -----------------------------------------------
>
> Key: GROOVY-11807
> URL: https://issues.apache.org/jira/browse/GROOVY-11807
> Project: Groovy
> Issue Type: Bug
> Components: groovy-jdk
> Reporter: Eric Milles
> Priority: Major
>
> {code}
> Map<Number,?> map = [:].withDefault{ null }
> assert map.get('x') == null
> Number k = map.keySet()[0]
> {code}
> GroovyCastException: Cannot cast object 'x' with class 'java.lang.String' to
> class 'java.lang.Number'
--
This message was sent by Atlassian Jira
(v8.20.10#820010)