Skip to content

Instantly share code, notes, and snippets.

@orangeclover
Created June 11, 2012 20:14
Show Gist options
  • Select an option

  • Save orangeclover/2912382 to your computer and use it in GitHub Desktop.

Select an option

Save orangeclover/2912382 to your computer and use it in GitHub Desktop.
Groovyで型を省略した場合と型を指定した場合の違い
/*
* 型を省略した場合
*/
//バインディング変数
v = "abc"
assert v.class == java.lang.String
v = 1
assert v == 1
assert v.class == java.lang.Integer
v = "xyz"
assert v.class == java.lang.String
//ローカル変数
def l = "abc"
assert l.class == java.lang.String
l = 1
assert l == 1
assert l.class == java.lang.Integer
l = "xyz"
assert l.class == java.lang.String
/*
* 型を指定した場合
*/
String s = "abc"
assert s.class == java.lang.String
s = 1
assert s.class == java.lang.String
assert s == "1"
def iTest = new IntegerTest()
iTest.test1()
iTest.test2()
class IntegerTest extends GroovyTestCase {
void test1() {
shouldFail(org.codehaus.groovy.runtime.typehandling.GroovyCastException) {
Integer i
i = "abc"
}
}
void test2() {
shouldFail(org.codehaus.groovy.runtime.typehandling.GroovyCastException) {
Integer i
i = "123"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment