Skip to content

Instantly share code, notes, and snippets.

@banterCZ
Last active January 29, 2016 10:28
Show Gist options
  • Save banterCZ/f647dd892e22103472e7 to your computer and use it in GitHub Desktop.
Save banterCZ/f647dd892e22103472e7 to your computer and use it in GitHub Desktop.
"Because Groovy dispatches by the runtime type, the specialized implementation of oracle(String) is used in the second case." "Wherever there’s a Java API that uses the static type Object, this code effectively loses the strength of static typing. ... This is why the Java type concept is called weak static typing" Source: Groovy in Action Second…
def oracle(Object o) { return 'object' }
def oracle(String o) { return 'string' }
Object x = 1
Object y = 'foo'
assert 'object' == oracle(x)
assert 'string' == oracle(y)
public class Dispatcher {
static String oracle(Object o) { return "object"; }
static String oracle(String o) { return "string"; }
public static void main(String... args) {
Object x = 1;
Object y = "foo";
assert "object".equals(oracle(x));
assert "object".equals(oracle(y));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment