On 03.08.2018 16:31, Paul King wrote:
[...]
However, for the case where the trait has the static method
and I override with an instance method, e.g.:
trait SFoo { static foo() { 'sfoo' } }
class SFooImpl implements SFoo {
def foo() { 'baz' }
}
assert new SFooImpl().foo() == 'baz'
It currently passes. I would expect that to be an error.
The standard rule is the implementation within the class
wins if one is found matching a trait but is it really a match?
well, since Java8 we have static methods in interfaces:
public interface X { static int foo(){return 1;}}
class Y implements X {
public int foo() { return 2;}
public static void main(String[] args) {
System.out.println(new Y().foo());
}
}
this will return 2, so I would say Groovy is doing the right thing here.
Similarly, this passes:
class SFooFoo implements SFoo, Foo {}
assert new SFooFoo().foo() == 'foo'
but this fails:
class FooSFoo implements Foo, SFoo {}
assert new FooSFoo().foo() == 'sfoo'
with:
The method 'java.lang.Object foo()' is already defined in class 'FooSFoo'.
You cannot have both a static and an instance method with the same signature
at line: -1, column: -1
>> I would expect both to fail. The standard rule is that the last one
wins
if there
are more than one of the same signature but I am not sure you can treat
these
as matching signatures.
my expectation is that the static method exists on the interface only.
And this means both should work.
bye Jochen