Re: reimplementing an interface in a derived class

2019-01-09 Thread Heromyth via Digitalmars-d-learn
On Thursday, 3 January 2019 at 23:23:12 UTC, Neia Neutuladh wrote: On Thu, 03 Jan 2019 22:30:48 +, kdevel wrote: class A : D { int foo() { return 1; } } class B : A, D { [...] What is the meaning of the ", D"? It does not seem to make a difference if it is omitted. B must provide

Re: reimplementing an interface in a derived class

2019-01-07 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/4/19 7:16 PM, kdevel wrote: On Friday, 4 January 2019 at 20:21:56 UTC, Steven Schveighoffer wrote: missing in the source. But why is d a null reference in the first place? Because when you dynamically cast one object or interface to another object or interface, and that result is not

Re: reimplementing an interface in a derived class

2019-01-04 Thread kdevel via Digitalmars-d-learn
On Friday, 4 January 2019 at 20:21:56 UTC, Steven Schveighoffer wrote: missing in the source. But why is d a null reference in the first place? Because when you dynamically cast one object or interface to another object or interface, and that result is not possible (if you remove ",D" from

Re: reimplementing an interface in a derived class

2019-01-04 Thread Ali Çehreli via Digitalmars-d-learn
On 01/04/2019 01:08 PM, Ali Çehreli wrote: > There is only one vtbl per class object Correcting myself after reading Neia Neutuladh's post: I should have said "There is only one vtbl per class type". Every class object has a vtbl pointer that points at its type's vtbl. So, it's normally two

Re: reimplementing an interface in a derived class

2019-01-04 Thread Alex via Digitalmars-d-learn
On Friday, 4 January 2019 at 21:47:59 UTC, Neia Neutuladh wrote: On Fri, 04 Jan 2019 08:46:24 +, Alex wrote: Let's assume this is right. How to force a B object to behave like an A object? I thought casting is a possible approach... It requires a bit of surgery: :) import

Re: reimplementing an interface in a derived class

2019-01-04 Thread Alex via Digitalmars-d-learn
On Friday, 4 January 2019 at 21:08:24 UTC, Ali Çehreli wrote: [...] In this case, casting is using the B object through it's A interface. The overridden behavior does not change. Yeah... This was my mistake. (Actually, that is possible in languages that support multiple inheritance through

Re: reimplementing an interface in a derived class

2019-01-04 Thread Neia Neutuladh via Digitalmars-d-learn
On Fri, 04 Jan 2019 08:46:24 +, Alex wrote: > Let's assume this is right. How to force a B object to behave like an A > object? I thought casting is a possible approach... It requires a bit of surgery: import std.stdio; class A { void foo() { writeln("hello from A!"); }

Re: reimplementing an interface in a derived class

2019-01-04 Thread Ali Çehreli via Digitalmars-d-learn
On 01/04/2019 12:46 AM, Alex wrote: > On Friday, 4 January 2019 at 07:37:43 UTC, bauss wrote: >> No, because you OVERRIDE A's foo(). >> >> A does not exist. A is B and when you cast B to A you just tell the >> compiler that the reference should only have A's signature available. >> >> You're not

Re: reimplementing an interface in a derived class

2019-01-04 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/4/19 2:55 PM, kdevel wrote: On Friday, 4 January 2019 at 11:27:59 UTC, Alex wrote: On Friday, 4 January 2019 at 09:58:59 UTC, bauss wrote: [...] As for the OP, I think here the usefulness of ", D" should be visible: [...] class B : A, D {     override int foo() { return 2; } }

Re: reimplementing an interface in a derived class

2019-01-04 Thread kdevel via Digitalmars-d-learn
On Friday, 4 January 2019 at 11:27:59 UTC, Alex wrote: On Friday, 4 January 2019 at 09:58:59 UTC, bauss wrote: [...] As for the OP, I think here the usefulness of ", D" should be visible: [...] class B : A, D { override int foo() { return 2; } } [...] D d = cast(D) b;

Re: reimplementing an interface in a derived class

2019-01-04 Thread Alex via Digitalmars-d-learn
On Friday, 4 January 2019 at 09:58:59 UTC, bauss wrote: On Friday, 4 January 2019 at 09:53:18 UTC, Alex wrote: I assume the move method of an Animal is not abstract, and therefore I supposed, casting to this type explicitly should restore this very non-abstract behavior. But this is not the

Re: reimplementing an interface in a derived class

2019-01-04 Thread bauss via Digitalmars-d-learn
On Friday, 4 January 2019 at 09:53:18 UTC, Alex wrote: I assume the move method of an Animal is not abstract, and therefore I supposed, casting to this type explicitly should restore this very non-abstract behavior. But this is not the case. And the final/virtual thing above explains this to

Re: reimplementing an interface in a derived class

2019-01-04 Thread Alex via Digitalmars-d-learn
On Friday, 4 January 2019 at 09:30:32 UTC, bauss wrote: Your C++ example is not the same as in D because in C++ functions aren't virtual by default, they are in D. Mark your functions as virtual in your C++ example and see what happens. All functions in D are virtual by default! Yep. Got

Re: reimplementing an interface in a derived class

2019-01-04 Thread Alex via Digitalmars-d-learn
On Friday, 4 January 2019 at 09:19:48 UTC, Simen Kjærås wrote: On Friday, 4 January 2019 at 08:40:04 UTC, Alex wrote: class A { public: int foo(){return 1;} }; class B : public A { public: int foo(){return 2;} }; In C++, methods are non-virtual by default. In D, they are virtual by

Re: reimplementing an interface in a derived class

2019-01-04 Thread bauss via Digitalmars-d-learn
On Friday, 4 January 2019 at 08:40:04 UTC, Alex wrote: On Friday, 4 January 2019 at 02:13:27 UTC, Neia Neutuladh wrote: I can't think of a single class system that works like that. C++, Java, C#, Dart, and TypeScript all work like D here. GObject in C works like D. In the example below, the

Re: reimplementing an interface in a derived class

2019-01-04 Thread Simen Kjærås via Digitalmars-d-learn
On Friday, 4 January 2019 at 08:40:04 UTC, Alex wrote: class A { public: int foo(){return 1;} }; class B : public A { public: int foo(){return 2;} }; In C++, methods are non-virtual by default. In D, they are virtual by default. Because of this, the two examples are different. In

Re: reimplementing an interface in a derived class

2019-01-04 Thread Alex via Digitalmars-d-learn
On Friday, 4 January 2019 at 07:37:43 UTC, bauss wrote: No, because you OVERRIDE A's foo(). A does not exist. A is B and when you cast B to A you just tell the compiler that the reference should only have A's signature available. You're not assigning B to A. Let's assume this is right.

Re: reimplementing an interface in a derived class

2019-01-04 Thread Alex via Digitalmars-d-learn
On Friday, 4 January 2019 at 02:13:27 UTC, Neia Neutuladh wrote: I can't think of a single class system that works like that. C++, Java, C#, Dart, and TypeScript all work like D here. GObject in C works like D. In the example below, the "2" of B.foo is printed only once. Independently of the

Re: reimplementing an interface in a derived class

2019-01-03 Thread bauss via Digitalmars-d-learn
On Friday, 4 January 2019 at 00:19:05 UTC, Alex wrote: On Friday, 4 January 2019 at 00:15:28 UTC, Neia Neutuladh wrote: On Thu, 03 Jan 2019 23:44:15 +, Alex wrote: I assume that is another bug and has nothing to do with interfaces... B.foo is both overriding A.foo and implementing D.foo,

Re: reimplementing an interface in a derived class

2019-01-03 Thread Neia Neutuladh via Digitalmars-d-learn
On Fri, 04 Jan 2019 00:19:05 +, Alex wrote: > B.foo overrides A.foo. By casting a B object to be an A object, A's > behavior should be granted, shouldn't it? I can't think of a single class system that works like that. C++, Java, C#, Dart, and TypeScript all work like D here. GObject in C

Re: reimplementing an interface in a derived class

2019-01-03 Thread Neia Neutuladh via Digitalmars-d-learn
On Thu, 03 Jan 2019 23:44:15 +, Alex wrote: > I assume that is another bug and has nothing to do with interfaces... B.foo is both overriding A.foo and implementing D.foo, so that's not a bug.

Re: reimplementing an interface in a derived class

2019-01-03 Thread Alex via Digitalmars-d-learn
On Friday, 4 January 2019 at 00:15:28 UTC, Neia Neutuladh wrote: On Thu, 03 Jan 2019 23:44:15 +, Alex wrote: I assume that is another bug and has nothing to do with interfaces... B.foo is both overriding A.foo and implementing D.foo, so that's not a bug. I don't have any interfaces in

Re: reimplementing an interface in a derived class

2019-01-03 Thread Alex via Digitalmars-d-learn
On Thursday, 3 January 2019 at 23:23:12 UTC, Neia Neutuladh wrote: On Thu, 03 Jan 2019 22:30:48 +, kdevel wrote: class A : D { int foo() { return 1; } } class B : A, D { [...] What is the meaning of the ", D"? It does not seem to make a difference if it is omitted. B must provide

Re: reimplementing an interface in a derived class

2019-01-03 Thread bauss via Digitalmars-d-learn
On Thursday, 3 January 2019 at 22:30:48 UTC, kdevel wrote: https://dlang.org/spec/interface.html #11 has this code example: ``` interface D { int foo(); } class A : D { int foo() { return 1; } } class B : A, D { override int foo() { return 2; } } ... B b = new B(); b.foo();

Re: reimplementing an interface in a derived class

2019-01-03 Thread Neia Neutuladh via Digitalmars-d-learn
On Thu, 03 Jan 2019 22:30:48 +, kdevel wrote: > class A : D { > int foo() { return 1; } > } > > class B : A, D { > [...] > > What is the meaning of the ", D"? It does not seem to make a difference > if it is omitted. B must provide its own implementation of D. It can't simply use A's