-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overriding a method as abstract #9998
Comments
When the abstract method has a stricter return type than the supertype's method: class Foo
def foo; end # Error: this method overrides Bar#foo() which has an explicit return type of Nil.
end
abstract class Bar < Foo
abstract def foo : Nil
end
class Baz < Bar; end Compared to the first example, only the return type |
Thanks for creating an issue for this! I'm fully on board with Java's semantics here. Regarding your follow-up comment, I have another question. Is there a reason that methods like |
Interesting fact, this already happens to class Foo
include Indexable(Nil)
def unsafe_fetch(index : Int)
end
end
Foo.new.size # Stack overflow (e.g., infinite or very deep recursion) |
For the record I verified that overriding from a sibling type works in Java only if implementation comes from a class and the visibility matches: class Foo {
public void foo() { }
}
interface Bar {
void foo();
}
class Baz extends Foo implements Bar { } // okay abstract class Foo {
abstract public void foo();
}
interface Bar {
default void foo() { }
}
class Baz extends Foo implements Bar { } // error: Baz is not abstract and does not override abstract method foo() in Foo interface Foo {
void foo();
}
interface Bar {
default void foo() { }
}
class Baz implements Foo, Bar { } // error: Baz is not abstract and does not override abstract method foo() in Foo |
Implementation in #11056. I'm not sure if we should change this directly to an error or start with a deprecation first. I'd consider code that inherits an abstract def implementation from a supertype broken. But it used to work until now. And it can easily happen if you inherit stdlib's |
You can define a method as abstract that is already defined by a supertype. The following code compiles:
I don't think that should compile. The intention of the abstract method is to force subtypes to implement it. When it's already defined in the supertype, that defeats the purpose.
So it would be better to make overriding a method as abstract an error to show the programmer this doesn't work as intended.
Or even better, actually override the method as abstract and force subtypes to implement it.
In Java for example, this works:
The text was updated successfully, but these errors were encountered: