August 10, 2019
On Saturday, 10 August 2019 at 14:29:03 UTC, John Colvin wrote:
> On Saturday, 10 August 2019 at 10:11:15 UTC, Alex wrote:
>> On Saturday, 10 August 2019 at 08:20:46 UTC, John Colvin wrote:
>>> On Friday, 9 August 2019 at 13:39:53 UTC, Simen Kjærås wrote:
>>>> <snip>
>>>
>>> Thanks for the extra detail.
>>>
>>> Is there a solid reason to ever use an interface over an abstract class? (Other than multiple inheritance).
>>>
>>> I'm such a noob at anything related to OO.
>>
>> The general question is tricky, as different languages differ in details what is forced and what is allowed for abstract classes and interfaces.
>>
>> But roughly speaking, my opinion is: if you can/want to provide some default behavior than you are about to write an abstract class.
>> If you are about to provide information/restriction of behavior, then this is more like an interface.
>
> Ok. What would go wrong (in D) if I just replaced every interface with an abstract class?

´´´
void main(){}
interface A { void fun(); }
abstract class B{ void fun(); }
class C : A{ void fun(){} }
class D : B{ /*override*/ void fun(){} }
´´´

case 1:
interface A and class C implementing interface A:
You don't need to "override" anything. You are forced to provide an implementation of the function inside the class.

case 2:
abstract class B and class D inheriting from it:
You can but not have to provide an implementation of a function inside the abstract class.
If I don't and do not provide any implementation inside D I get a linker error. Don't how this case behaves on your system.
If you provide an implementation inside the abstract class, you don't have to provide any in the derived one.
In any case, if you want to provide an implementation inside the derived class you have to literally "override", as in D implicit overrides are not allowed.
August 10, 2019
On 10.08.19 16:29, John Colvin wrote:
> 
> Ok. What would go wrong (in D) if I just replaced every interface with an abstract class?

interface A{}
interface B{}

class C: A,B{ }
August 11, 2019
On Saturday, 10 August 2019 at 17:46:37 UTC, Timon Gehr wrote:
> On 10.08.19 16:29, John Colvin wrote:
>> 
>> Ok. What would go wrong (in D) if I just replaced every interface with an abstract class?
>
> interface A{}
> interface B{}
>
> class C: A,B{ }

Yes, I know, I guess it wasn't clear unless you read my previous question, I said "apart from multiple inheritance".
August 11, 2019
On Saturday, 10 August 2019 at 17:28:32 UTC, Alex wrote:
>
> ´´´
> void main(){}
> interface A { void fun(); }
> abstract class B{ void fun(); }
> class C : A{ void fun(){} }
> class D : B{ /*override*/ void fun(){} }
> ´´´
>
> case 1:
> interface A and class C implementing interface A:
> You don't need to "override" anything. You are forced to provide an implementation of the function inside the class.
>
> case 2:
> abstract class B and class D inheriting from it:
> You can but not have to provide an implementation of a function inside the abstract class.
> If I don't and do not provide any implementation inside D I get a linker error. Don't how this case behaves on your system.
> If you provide an implementation inside the abstract class, you don't have to provide any in the derived one.
> In any case, if you want to provide an implementation inside the derived class you have to literally "override", as in D implicit overrides are not allowed.

I think there's some confusion here, because B.foo is not abstract. abstract on a class is not inherited by its methods. https://dlang.org/spec/attribute.html#abstract

August 11, 2019
On Sunday, 11 August 2019 at 13:09:43 UTC, John Colvin wrote:
>> Ok. What would go wrong (in D) if I just replaced every interface with an abstract class?
>
> I think there's some confusion here, because B.foo is not abstract. abstract on a class is not inherited by its methods. https://dlang.org/spec/attribute.html#abstract

Now, I'm confused, as you asked about abstract classes. So, yes, you can define the abstractness of classes differently. And what is your point?
August 11, 2019
On Sunday, 11 August 2019 at 15:16:03 UTC, Alex wrote:
> On Sunday, 11 August 2019 at 13:09:43 UTC, John Colvin wrote:
>>> Ok. What would go wrong (in D) if I just replaced every interface with an abstract class?
>>
>> I think there's some confusion here, because B.foo is not abstract. abstract on a class is not inherited by its methods. https://dlang.org/spec/attribute.html#abstract
>
> Now, I'm confused, as you asked about abstract classes. So, yes, you can define the abstractness of classes differently. And what is your point?

I'm trying to narrow down exactly what patterns work with each and how they overlap.

What I was trying to get at with the abstract method thing is that

abstract class C
{
    void foo();
}

is an abstract class with a non-abstract method, whose implementation is going to come from somewhere else (not a common pattern in D).

class C
{
    abstract void foo();
}

is an abstract class with an abstract method foo, which means you have to override it in a inheriting class to get a non-abstract class.
August 11, 2019
On Sunday, 11 August 2019 at 16:05:20 UTC, John Colvin wrote:
> I'm trying to narrow down exactly what patterns work with each and how they overlap.
>
> What I was trying to get at with the abstract method thing is that
>
> abstract class C
> {
>     void foo();
> }
>
> is an abstract class with a non-abstract method, whose implementation is going to come from somewhere else (not a common pattern in D).
>
> class C
> {
>     abstract void foo();
> }
>
> is an abstract class with an abstract method foo, which means you have to override it in a inheriting class to get a non-abstract class.

As I see this, everything you wrote is correct. :)

But you compared abstractness with interface usage, initially. So... I would say, interfaces are more like the abstract method case without any function body. But then, you will have to use "override" all across the inherited classes.
August 11, 2019
On Sunday, 11 August 2019 at 20:15:34 UTC, Alex wrote:
> On Sunday, 11 August 2019 at 16:05:20 UTC, John Colvin wrote:
>> I'm trying to narrow down exactly what patterns work with each and how they overlap.
>>
>> What I was trying to get at with the abstract method thing is that
>>
>> abstract class C
>> {
>>     void foo();
>> }
>>
>> is an abstract class with a non-abstract method, whose implementation is going to come from somewhere else (not a common pattern in D).
>>
>> class C
>> {
>>     abstract void foo();
>> }
>>
>> is an abstract class with an abstract method foo, which means you have to override it in a inheriting class to get a non-abstract class.
>
> As I see this, everything you wrote is correct. :)
>
> But you compared abstractness with interface usage, initially. So... I would say, interfaces are more like the abstract method case without any function body. But then, you will have to use "override" all across the inherited classes.

Ok. So that means the difference is pretty subtle, give or take a few extra keywords.

Which leaves multiple inheritance as the only significant difference?

From my perspective it looks like there are two massively overlapping features with some quite arbitrary feeling restrictions and differences. E.g. why can I not inherit from multiple 100% abstract empty classes? Wouldn't that be the same as inheriting from multiple interfaces?
August 11, 2019
On Sunday, 11 August 2019 at 20:32:14 UTC, John Colvin wrote:
> E.g. why can I not inherit from multiple 100% abstract empty classes? Wouldn't that be the same as inheriting from multiple interfaces?

There's kinda no such thing as 100% empty abstract classes, since they all have the implicit parent of Object with its associated pieces. D's interfaces have no implicit parent.

But if that were to change, then yeah, it should work - that's basically what C++ does in lieu of interfaces.

(I personally prefer separate interfaces anyway, as it makes the intention clear and thus can help with better error messages and documentation, but it would work.)
August 11, 2019
On Sunday, August 11, 2019 2:32:14 PM MDT John Colvin via Digitalmars-d- learn wrote:
> On Sunday, 11 August 2019 at 20:15:34 UTC, Alex wrote:
> > As I see this, everything you wrote is correct. :)
> >
> > But you compared abstractness with interface usage, initially. So... I would say, interfaces are more like the abstract method case without any function body. But then, you will have to use "override" all across the inherited classes.
>
> Ok. So that means the difference is pretty subtle, give or take a few extra keywords.
>
> Which leaves multiple inheritance as the only significant difference?
>
>  From my perspective it looks like there are two massively
> overlapping features with some quite arbitrary feeling
> restrictions and differences. E.g. why can I not inherit from
> multiple 100% abstract empty classes? Wouldn't that be the same
> as inheriting from multiple interfaces?

Well, as things stand, _no_ class is 100% abstract, because they all derive from Object, and Object has virtual functions on it with implementations, whereas an interface _is_ 100% abstract. Maybe once we have ProtoObject, it could be argued for allowing 100% abstract classes to be treated as interfaces, but right now, that wouldn't be possible, and even with ProtoObject, it arguably wouldn't be worth the extra complication, since if you really intend to have a 100% abstract class, that's what interfaces are for.

There's also the weird complications that come with D's COM support, since that uses interfaces but gets treated differently from how classes are normally treated, and I don't know how that affects the implementation of interfaces. If we'd had ProtoObject from the get-go, I wonder if it would have just been better to implement COM interfaces as being derived from a specific class that's derived from ProtoObject instead of mucking up interfaces the way that we currently hove, but I don't know. I haven't ever actually used D's COM support, so I don't fully understand it.

For the most, D's interfaces and abstract classes follow what you get in Java and C# (though the weirdness with COM is unique to D as is the ability to have final functions with an implementation). Basically, it seems like what we got with interfaces and abstract classes was copied from Java and then tweaked. I suspect that the separation between interfaces and abstract classes in Java comes from the fact that Object has functions on it, and that same logic carried over to D.

In practice, what I would expect to typically happen is that if you're defining a set of functions that a class needs to have but not providing any implementations, then you'd use an interface, whereas if you intend to provide an implementation for any part of it but not all of it, you'd use an abstract class. I wouldn't expect abstract classes with no functions (outside of those from Object) or variables being declared on them to be used much. Maybe someone would have a use case where it would make sense to have a common base class with no implementations, but I can't think of any reason why that would be useful other than preventing classes from being derived from any class from a different class hierarchy, which isn't usually something that's worth preventing.

Regardless, the whole weirdness that you're running into with void* is not something that much code would care about, because very little code is going to do something like cast a reference to void*, and the code that does do that is @system and expected to deal with it correctly. In general, casting to void* and then casting to anything other than the original type is probably asking for trouble. If I understand correctly, with void*, you're basically doing a reinterpet cast, and that's not usually the type of cast you want when dealing with class references. If I had code where whether casting to an interface or abstract class mattered, I'd want to redesign it so that that didn't matter.

- Jonathan M Davis