groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul King <pa...@asert.com.au>
Subject [DISCUSS] trait behavior with instance and static methods of otherwise same signature
Date Fri, 03 Aug 2018 14:31:47 GMT
There are some trait edge cases which I think we can improve.

If I have a trait and override by a method in my class
with the same signature except static, e.g.:

trait Foo { def foo() { 'foo' } }
class FooImpl implements Foo {
    static foo() { 'bar' }
}
assert new FooSFoo().foo() == 'bar'

you currently get an error message like:

The method 'java.lang.Object foo()' is already defined in class 'FooImpl'.
You cannot have both a static and an instance method with the same signature

This seems okay to me although perhaps could be made clearer
by detecting during trait composition rather than being caught during
later verification.

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?

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.

 What do others think?

Cheers, Paul.

Mime
View raw message