digitalmars.D.learn - implimenting interface function by inheriting from other class
- Alexey (25/25) Aug 21 2021 Hello
- Bastiaan Veelo (14/39) Aug 21 2021 Not sure if this is the best way, but it does make dmd happy:
- Alexey (3/47) Aug 21 2021 I want `this` inside of C1::coolFunc to return C2 if called as
- Alexey (89/114) Aug 21 2021 we're with friends tried similar thing with c++, java, groovy and
- Tejas (70/189) Aug 21 2021 Sorry, Bastiaan's solution is the only that I know of as well.
- Alexey (2/27) Aug 22 2021 so, Is this a bug in dmd or not?
- Alexey (4/36) Aug 22 2021 some answers have been given here and here
Hello ```D interface Int { void coolFunc(); } class C1 { void coolFunc() { return; } } class C2 : C1, Int { } void main() { auto c = new C2; } ``` dmd says it's not Ok: t.d(14): Error: class `t.C2` interface function `void coolFunc()` is not implemented how to make dmd happy?
Aug 21 2021
On Saturday, 21 August 2021 at 20:35:43 UTC, Alexey wrote:Hello ```D interface Int { void coolFunc(); } class C1 { void coolFunc() { return; } } class C2 : C1, Int { } void main() { auto c = new C2; } ``` dmd says it's not Ok: t.d(14): Error: class `t.C2` interface function `void coolFunc()` is not implemented how to make dmd happy?Not sure if this is the best way, but it does make dmd happy: https://run.dlang.io/is/44F3AE ```d class C2 : C1, Int { override void coolFunc() { C1.coolFunc; } } ``` It looks lame, I admit. — Bastiaan.
Aug 21 2021
On Saturday, 21 August 2021 at 22:56:40 UTC, Bastiaan Veelo wrote:On Saturday, 21 August 2021 at 20:35:43 UTC, Alexey wrote:I want `this` inside of C1::coolFunc to return C2 if called as C2::coolFuncHello ```D interface Int { void coolFunc(); } class C1 { void coolFunc() { return; } } class C2 : C1, Int { } void main() { auto c = new C2; } ``` dmd says it's not Ok: t.d(14): Error: class `t.C2` interface function `void coolFunc()` is not implemented how to make dmd happy?Not sure if this is the best way, but it does make dmd happy: https://run.dlang.io/is/44F3AE ```d class C2 : C1, Int { override void coolFunc() { C1.coolFunc; } } ``` It looks lame, I admit. — Bastiaan.
Aug 21 2021
I want `this` inside of C1::coolFunc to return C2 if called as C2::coolFuncso executing `cast(C2) this !is null` inside of C1::coolFunc would work
Aug 21 2021
On Saturday, 21 August 2021 at 23:14:14 UTC, Alexey wrote:If this would work, I'd farther used this like so ```D interface Int { void coolFunc(); } class C1 { void coolFunc() { { auto obj = (cast(C1)(cast(Int) this)); if (obj !is null) { do_one_thing(); } } { auto obj = (cast(C2)(cast(Int) this)); if (obj !is null) { do_another_thing(); } } { auto obj = (cast(C3)(cast(Int) this)); if (obj !is null) { do_something_else(); } } return; } } class C2 : C1, Int { } class C3 : C1, Int { } class C4 : C1, Int { } void main() { auto c = new C2; c.coolFunc(); } ```I want `this` inside of C1::coolFunc to return C2 if called as C2::coolFuncso executing `cast(C2) this !is null` inside of C1::coolFunc would work
Aug 21 2021
On Saturday, 21 August 2021 at 20:35:43 UTC, Alexey wrote:Hello ```D interface Int { void coolFunc(); } class C1 { void coolFunc() { return; } } class C2 : C1, Int { } void main() { auto c = new C2; } ``` dmd says it's not Ok: t.d(14): Error: class `t.C2` interface function `void coolFunc()` is not implemented how to make dmd happy?we're with friends tried similar thing with c++, java, groovy and go - worked ok. ```Java interface Int { public void coolFunc(); }; class C1 { public void coolFunc() { return; } }; class C2 extends C1 implements Int { }; public class Main { public static void main(String args[]) { Int c = new C2(); } } ``` ```Groovy interface Int { void coolFunc() } class C1 { void coolFunc() { print "hello" } } class C2 extends C1 implements Int { } def printHello = { Int a -> a.coolFunc(); print " world: accept $Int" } C2 a = new C2() printHello(a) ``` ```C++ class Int; class C1; class C2; class Int { public: virtual void coolFunc() = delete; }; class C1 { public: virtual void coolFunc() { return; } }; class C2 : public C1, public Int { public: }; int main(int argc, char* argv[]) { auto c = new C2; } ``` ```Go package main import "fmt" type Int interface { CoolFunc() } type C1 struct { } func (self *C1) CoolFunc() { fmt.Println("worked") return } type C2 struct { C1 } func main() { var c Int c = new(C2) c.CoolFunc() } ```
Aug 21 2021
On Sunday, 22 August 2021 at 01:14:08 UTC, Alexey wrote:On Saturday, 21 August 2021 at 20:35:43 UTC, Alexey wrote:Sorry, Bastiaan's solution is the only that I know of as well. Here's your desired behaviour: ```d import std.stdio:writeln; interface Int { void coolFunc(); } class C1 { void coolFunc() { { auto obj = (cast(C1)(cast(Int) this)); if (obj !is null) { do_one_thing(); } } { auto obj = (cast(C2)(cast(Int) this)); if (obj !is null) { obj.do_another_thing(); } } { auto obj = (cast(C3)(cast(Int) this)); if (obj !is null) { obj.do_something_else(); } } return; } void do_one_thing(){ writeln("one thing from C1"); } } class C2 : C1, Int { override void coolFunc(){ typeof(super).coolFunc(); } void do_another_thing(){ writeln("did another thing from C2"); } } class C3 : C1, Int { override void coolFunc(){ typeof(super).coolFunc(); } void do_something_else(){ writeln("doing something else from C3"); } } class C4 : C1, Int { override void coolFunc(){ typeof(super).coolFunc(); } } void main() { auto c = new C2; c.coolFunc(); } ```Hello ```D interface Int { void coolFunc(); } class C1 { void coolFunc() { return; } } class C2 : C1, Int { } void main() { auto c = new C2; } ``` dmd says it's not Ok: t.d(14): Error: class `t.C2` interface function `void coolFunc()` is not implemented how to make dmd happy?we're with friends tried similar thing with c++, java, groovy and go - worked ok. ```Java interface Int { public void coolFunc(); }; class C1 { public void coolFunc() { return; } }; class C2 extends C1 implements Int { }; public class Main { public static void main(String args[]) { Int c = new C2(); } } ``` ```Groovy interface Int { void coolFunc() } class C1 { void coolFunc() { print "hello" } } class C2 extends C1 implements Int { } def printHello = { Int a -> a.coolFunc(); print " world: accept $Int" } C2 a = new C2() printHello(a) ``` ```C++ class Int; class C1; class C2; class Int { public: virtual void coolFunc() = delete; }; class C1 { public: virtual void coolFunc() { return; } }; class C2 : public C1, public Int { public: }; int main(int argc, char* argv[]) { auto c = new C2; } ``` ```Go package main import "fmt" type Int interface { CoolFunc() } type C1 struct { } func (self *C1) CoolFunc() { fmt.Println("worked") return } type C2 struct { C1 } func main() { var c Int c = new(C2) c.CoolFunc() } ```
Aug 21 2021
On Saturday, 21 August 2021 at 20:35:43 UTC, Alexey wrote:Hello ```D interface Int { void coolFunc(); } class C1 { void coolFunc() { return; } } class C2 : C1, Int { } void main() { auto c = new C2; } ``` dmd says it's not Ok: t.d(14): Error: class `t.C2` interface function `void coolFunc()` is not implemented how to make dmd happy?so, Is this a bug in dmd or not?
Aug 22 2021
On Sunday, 22 August 2021 at 12:20:56 UTC, Alexey wrote:On Saturday, 21 August 2021 at 20:35:43 UTC, Alexey wrote:some answers have been given here and here https://discord.com/channels/242094594181955585/242122752436338688/878996818904612864 https://issues.dlang.org/show_bug.cgi?id=22232Hello ```D interface Int { void coolFunc(); } class C1 { void coolFunc() { return; } } class C2 : C1, Int { } void main() { auto c = new C2; } ``` dmd says it's not Ok: t.d(14): Error: class `t.C2` interface function `void coolFunc()` is not implemented how to make dmd happy?so, Is this a bug in dmd or not?
Aug 22 2021