I ended up putting this in the Java vs Groovy section instead of where I
originally planned because it occurred to me that these conversions apply
to more than just assignment (e.g. method invocation). Here's what I have
so far: https://github.com/apache/incubator-groovy/pull/98.
Besides wanting any feedback you might have (especially on other options
for the second table), I also wondered if these behaviors were intentional:
- char -> byte requires a cast in Java, but doesn't in Groovy (even with
CompileStatic)
- Most boxing/unboxing works with CompileStatic, but not char ->
Character or Character -> char
- Several inconsistencies around when casts are needed when working with
CompileStatic (e.g. short -> int and short -> Integer, and char -> int
work, but not char -> Integer)
For reference, I used this script to help me get a jumpstart on the table
(though obviously it doesn't detect most truncations):
types = ['boolean', 'Boolean', 'byte', 'Byte', 'short', 'Short',
'char', 'Character', 'int', 'Integer', 'long', 'Long', 'BigInteger',
'float', 'Float', 'double', 'Double', 'BigDecimal']
GroovyClassLoader groovyClassLoader = new GroovyClassLoader()
String[] params = new String[1]
for (def fromType in types) {
for (def toType in types) {
def code = """class Main{static void main(def args){
$fromType foo = 0
$toType bar = foo
}}"""
try {
groovyClassLoader.parseClass("@groovy.transform.CompileStatic\n"
+ code).getMethod("main", String[].class).invoke(null, params)
if (fromType == toType) {
println "${fromType}\t${toType}\t-"
continue
}
if (fromType.toLowerCase().startsWith(toType) ||
toType.toLowerCase().startsWith(fromType)) {
println "${fromType}\t${toType}\tB"
continue
}
if (toType.toLowerCase() == 'boolean') {
println "${fromType}\t${toType}\tT"
continue
}
println "${fromType}\t${toType}\tY"
} catch(e1) {
try {
groovyClassLoader.parseClass(code).getMethod("main",
String[].class).invoke(null, params)
println "${fromType}\t${toType}\tD"
} catch(e2) {
println "${fromType}\t${toType}\tN"
}
}
}
}
-Keegan
On Tue, Aug 11, 2015 at 1:07 PM, Keegan Witt <[email protected]> wrote:
> I didn't see that we had this documented anywhere, even in GINA (if I've
> missed it, please point it out). Where do you think it'd be appropriate to
> document the behavior assignments like below? A subsection under the
> variable assignment section
> <http://docs.groovy-lang.org/next/html/documentation/#_variable_assignment>
> ?
>
> Double a = new BigDecimal(0)
> Double b = new BigInteger(0)
> Float c = new BigDecimal(0)
> Float d = new BigInteger(0)
> Float e = new Double(0)
> Long f = new BigDecimal(0)
> Long g = new BigInteger(0)
> Long h = new Double(0)
> Long i = new Float(0)
> Integer j = new BigDecimal(0)
> Integer k = new BigInteger(0)
> Integer l = new Double(0)
> Integer m = new Float(0)
> Integer n = new Long(0)
> Short o = new BigDecimal(0)
> Short p = new BigInteger(0)
> Short q = new Double(0)
> Short r = new Float(0)
> Short s = new Long(0)
> Short t = new Integer(0)
> Byte u = new BigDecimal(0)
> Byte v = new Double(0)
> Byte w = new Float(0)
> Integer x = "A"
>
> -Keegan
>