[groovy-user] How to use multiple return values with @CompileStatic?

905 views
Skip to first unread message

ngala...@abinitio.com

unread,
Jan 15, 2015, 4:15:10 PM1/15/15
to us...@groovy.codehaus.org
Hello,

In a class that is @CompileStatic, if I have:

void foo()
{
  int i
 String s
 (i, s) = intAndStr()
...

}

and

List intAndStr()
{
 [
3, "hi"]
}


I get the compilation error:

Error:(700, 14) Groovyc: [Static type checking] - Multiple assignments without list expressions on the right hand side are unsupported in static type checking mode

I'm having trouble interpreting the error message. To me, intAndStr() looks like a "list expression".

Does this mean that multiple return values are not supported at all under @CompileStatic?

If they are supported, how do I declare intAndStr()?


Thanks,

Neil



NOTICE from Ab Initio: This email (including any attachments) may contain information that is subject to confidentiality obligations or is legally privileged, and sender does not waive confidentiality or privilege. If received in error, please notify the sender, delete this email, and make no further use, disclosure, or distribution.

Cédric Champeau

unread,
Jan 15, 2015, 4:21:50 PM1/15/15
to us...@groovy.codehaus.org
On 15/01/2015 22:13, ngala...@ABINITIO.COM wrote:
Hello,

In a class that is @CompileStatic, if I have:

void foo()
{
  int i
 String s
 (i, s) = intAndStr()
...

}

and

List intAndStr()
{
 [
3, "hi"]
}


I get the compilation error:

Error:(700, 14) Groovyc: [Static type checking] - Multiple assignments without list expressions on the right hand side are unsupported in static type checking mode

I'm having trouble interpreting the error message. To me, intAndStr() looks like a "list expression".
The error message should probably say "list literal"


Does this mean that multiple return values are not supported at all under @CompileStatic?
Correct. Note that technically you are using a List as a return type, but it doesn't say anything about the size of the list, nor the contents of the list, so the type checker cannot support this. We would need tuple types for that (and probably the upcoming value types of JDK 9).



If they are supported, how do I declare intAndStr()?


Thanks,

Neil



NOTICE from Ab Initio: This email (including any attachments) may contain information that is subject to confidentiality obligations or is legally privileged, and sender does not waive confidentiality or privilege. If received in error, please notify the sender, delete this email, and make no further use, disclosure, or distribution.


-- 
Cédric Champeau
Groovy language developer
http://twitter.com/CedricChampeau
http://melix.github.io/blog

ngala...@abinitio.com

unread,
Jan 15, 2015, 4:31:31 PM1/15/15
to us...@groovy.codehaus.org
Cedric,

Thank you for the explanation.


Neil
Reply all
Reply to author
Forward
0 new messages