[
https://issues.apache.org/jira/browse/GROOVY-9885?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Eric Milles updated GROOVY-9885:
--------------------------------
Fix Version/s: 2.5.16
> STC doesn't coerce GString in certain ternary expressions
> ---------------------------------------------------------
>
> Key: GROOVY-9885
> URL: https://issues.apache.org/jira/browse/GROOVY-9885
> Project: Groovy
> Issue Type: Bug
> Components: Static Type Checker
> Affects Versions: 3.0.7
> Reporter: Christopher Smith
> Assignee: Eric Milles
> Priority: Minor
> Fix For: 3.0.8, 4.0.0-alpha-3, 2.5.16
>
> Time Spent: 0.5h
> Remaining Estimate: 0h
>
> I know there are a number of edge cases regarding automatic coercion from
> GString to String, so I'm not certain whether this is a specific case of a
> larger problem area, but I've been able to isolate it.
> When using the named-parameter constructor approach, ternary expressions
> don't "inherit" the target type, and thus the inferred type of the expression
> is {{Serializable}} (not even {{CharSequence}}!). The problem is identical
> whether elvis or the long form is used.
> {code:groovy}
> @CompileStatic
> class Elvis {
> static class HasString {
> String value
> }
> void foo(String a, String b, String c) {
> new HasString(
> value: (a ?: "$b $c")
> )
> }
> }
> {code}
> {code}
> Cannot assign value of type java.io.Serializable <? extends java.lang.Object>
> to variable of type java.lang.String
> {code}
> This doesn't fail when used as
> {code}
> new HasString().value =
> {code} An explicit {{as String}} makes everyone happy but shouldn't be needed.
--
This message was sent by Atlassian Jira
(v8.20.1#820001)