-1
I’m converting an old Java application to Groovy. The syntax conversion is smooth. The problem is dynamic Groovy typification.
Let me give you an example, I declare a method foo
and its overload:
int foo(String arg) {
return 1;
}
int foo(Object arg) {
return 2;
}
Then I apply the method foo
on an instance of the type Object
what reference a type String
:
Object obj = "PT Stack Overflow";
int resultado = foo(obj);
Applying the method in Java System.out.println()
or the method in Groovy println()
get different results for each language:
In Java:
System.out.println("Em Java o resultado é " + resultado );
> Em Java o resultado é 2
In Groovy:
println("Em Groovy o resultado é " + resultado );
> Em Groovy o resultado é 1
This is because the Java and Groovy overloaded methods dispatch system are distinct. Java chooses the method according to the signature of the parameters and Groovy uses a type self-promotion system.
I would like to know how I do to disable this dynamic typing system when invoking methods because in my application I have portions of code where there are overloaded methods that perform the same task, however, based on the guy of the parameter passed the program opens distinct subforms and the Groovy conversion is practically useless.
You can’t specialize anymore
int foo(Object arg)
, with an interface, for example?– sbrubes
How would using an interface modify dynamic type checking? From what I know an interface only stipulates a minimum set of methods to be implemented by a class.
– Augusto Vasques
Who negatively could comment and help improve my question?
– Augusto Vasques