Bug 2565 – Should be able to use an inherited method as interface implementation

Status
REOPENED
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2009-01-07T14:35:37Z
Last change time
2024-12-13T17:49:20Z
Assigned to
No Owner
Creator
Koroskin Denis
Moved to GitHub: dmd#17858 →

Comments

Comment #0 by 2korden — 2009-01-07T14:35:37Z
interface IFoo { int foo(); } class FooImpl : IFoo { int foo() { return 42; } } class Foo : public FooImpl, public IFoo { } void main() { Foo foo = new Foo(); } Error: class A.Foo interface function IFoo.foo is not implemented I believe the following code sample should just work. Otherwise, one have to explicitly override all the interface methods, which is redundant and adds runtime cost. It could be related to bug 2539.
Comment #1 by shro8822 — 2009-01-07T14:38:04Z
This is working as designed. It chould be converted to "enhancement" or closed as invalid
Comment #2 by 2korden — 2009-01-07T14:48:53Z
(In reply to comment #1) > This is working as designed. It chould be converted to "enhancement" or closed > as invalid > Okay, but I didn't find it in docs. I know it is not a convincing argument, but C# allows that.
Comment #3 by smjg — 2009-01-07T15:13:55Z
*** Bug 2539 has been marked as a duplicate of this bug. ***
Comment #4 by shro8822 — 2009-01-07T15:22:29Z
It doesn't say it explicitly but it does say: "All interface functions must be defined in a class that inherits from that interface:" and "A reimplemented interface must implement all the interface functions, it does not inherit them from a super class:" Could be better. BTW: if you alias an existing function than it will fill in an interface. I think.
Comment #5 by smjg — 2009-01-07T15:23:01Z
I think the reason for this design is to prevent accidental implementation of an interface by an inherited method with completely different semantics. So, AISI, it should be possible to use an inherited method that happens to have the required name as an interface implementation, including in the case where the inherited method is final, but the means should be explicit. Issue 502 already describes one possibility.
Comment #6 by andrej.mitrovich — 2013-02-04T17:42:55Z
(In reply to comment #5) > but the means should be explicit. This could be implementable via a mixin template, similar to how forwarding constructors were proposed in Issue9066. A general-purpose template could be written which could be used via: mixin Forward!(BaseClass, "funcName"); And another template which uses it to fix this issue with: class Foo : public FooImpl, public IFoo { mixin ImplementInterface!(IFoo, FooImpl); // which expands to: mixin Forward!(FooImpl, "foo"); mixin Forward!(FooImpl, "bar"); // which expands to: override int foo() { return FooImpl.foo(); } override int bar() { return FooImpl.bar(); } } Internally it would iterate through all IFoo interface functions and find the matching implementations in FooImpl, and use `Forward` to mixin in a forwarding function. Or we would have language support. Anyway it should be doable as a library solution for the time being.
Comment #7 by smjg — 2013-02-05T14:03:18Z
(In reply to comment #6) > (In reply to comment #5) > > but the means should be explicit. > > This could be implementable via a mixin template, similar to how forwarding > constructors were proposed in Issue9066. A general-purpose template could be > written which could be used via: <snip> > // which expands to: > override int foo() { return FooImpl.foo(); } > override int bar() { return FooImpl.bar(); } Why do you need a mixin to do this? ISTM you might as well just insert these directly in your code. In any case, what you're suggesting doesn't seem to me to be an explicit way of using _an_ inherited method as _an_ interface implementation. Moreover, how does it accommodate the case where FooImpl.foo is final?
Comment #8 by andrej.mitrovich — 2013-02-05T14:09:31Z
(In reply to comment #7) > (In reply to comment #6) > > (In reply to comment #5) > > > but the means should be explicit. > > > > This could be implementable via a mixin template, similar to how forwarding > > constructors were proposed in Issue9066. A general-purpose template could be > > written which could be used via: > <snip> > > // which expands to: > > override int foo() { return FooImpl.foo(); } > > override int bar() { return FooImpl.bar(); } > > Why do you need a mixin to do this? ISTM you might as well just insert these > directly in your code. In any case, what you're suggesting doesn't seem to me > to be an explicit way of using _an_ inherited method as _an_ interface > implementation. Moreover, how does it accommodate the case where FooImpl.foo > is final? Ok fair enough, the mixin definitely has its share of problems.
Comment #9 by smjg — 2019-08-20T21:03:53Z
No explanation given for marking INVALID. Reopening.
Comment #10 by robert.schadek — 2024-12-13T17:49:20Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17858 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB