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 2023/02/28 03:26:00 UTC

[jira] [Created] (GROOVY-10954) Flow typing anomaly with multi-assignment

Paul King created GROOVY-10954:
----------------------------------

             Summary: Flow typing anomaly with multi-assignment
                 Key: GROOVY-10954
                 URL: https://issues.apache.org/jira/browse/GROOVY-10954
             Project: Groovy
          Issue Type: Improvement
            Reporter: Paul King


The following method compiles fine. Flow typing is used for type inference on {{a}}.
{code}
@groovy.transform.TypeChecked
def method1() {
    def a = 42
    assert a.intValue() == 42
    a = 'ant'
    assert a.toUpperCase() == 'ANT'
}
method1()
{code}

The following method, which differs only that multi-assignment (with one assignment in this case) is in use, fails to compile:
{code}
@groovy.transform.TypeChecked
def method2() {
    def (b) = [42]
    assert b.intValue() == 42
    (b) = ['bee']     // Cannot assign value of type java.lang.String to variable of type int
    assert b.toUpperCase() == 'BEE'    // Cannot find matching method int#toUpperCase()
}
method2()
{code}
I can't recall any discussion about why we wouldn't support the second example.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)