www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - abstract class information

reply Wulfklaue <wulfklaue wulfklaue.com> writes:
I am just looking up abstract class information and i notice that 
there is no information on dlang.org/spec

Yet using this as a resource:

https://www.tutorialspoint.com/d_programming/d_programming_abstract_classes.htm

It can be found, with a proper example. Am i blind and not seeing 
it in the specs?

Even Ali's book barely mentions it ( 55 ).

And yet abstract classes exit and function in D. So strange.
May 30 2017
next sibling parent reply Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Tuesday, May 30, 2017 22:13:54 Wulfklaue via Digitalmars-d wrote:
 I am just looking up abstract class information and i notice that
 there is no information on dlang.org/spec

 Yet using this as a resource:

 https://www.tutorialspoint.com/d_programming/d_programming_abstract_classe
 s.htm

 It can be found, with a proper example. Am i blind and not seeing
 it in the specs?

 Even Ali's book barely mentions it ( 55 ).

 And yet abstract classes exit and function in D. So strange.
http://dlang.org/spec/attribute.html#abstract That part of the spec talks about the class becoming abstract if any of its members are. Note that marking a class as abstract is equivalent to marking all of its member functions with abstract, just like marking class with safe would make all of its member functions safe. So, there isn't really any special handling of marking the class itself as abstract. - Jonathan M Davis
May 30 2017
parent reply ag0aep6g <anonymous example.com> writes:
On 05/31/2017 12:19 AM, Jonathan M Davis via Digitalmars-d wrote:
 Note that marking a class as abstract is equivalent to marking
 all of its member functions with abstract, just like marking class with
  safe would make all of its member functions  safe. So, there isn't really
 any special handling of marking the class itself as abstract.
That's not true. A class's `abstract` attribute does not transfer to its methods. It's the other way around: Having an abstract method makes the class abstract.
May 30 2017
parent reply Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Wednesday, May 31, 2017 07:30:20 ag0aep6g via Digitalmars-d wrote:
 On 05/31/2017 12:19 AM, Jonathan M Davis via Digitalmars-d wrote:
 Note that marking a class as abstract is equivalent to marking
 all of its member functions with abstract, just like marking class with
  safe would make all of its member functions  safe. So, there isn't
 really any special handling of marking the class itself as abstract.
That's not true. A class's `abstract` attribute does not transfer to its methods. It's the other way around: Having an abstract method makes the class abstract.
Hmmm. That would make abstract inconsistent with other attributes on a class, but after messing around with it a bit, it looks like the only effect that marking a class with abstract has is how many times you get a linker error about the member function not being defined. So, it looks like a I did indeed remember incorrectly (or just misunderstood), but the behavior that we do get with regards to putting abstract on the class is rather weird as well as inconsistent with what happens with other attributes on a class. But on reflection, that's probably because you wouldn't want to mark a function with a body as abstract, meaning that marking abstract on a class would either have to be ignored as it seems to be, or it would have to be ignored on any function that has a body, which would probably cause problems with .di files. So, I stand corrected. - Jonathan M Davis
May 31 2017
parent Jacob Carlborg <doob me.com> writes:
On 2017-05-31 09:09, Jonathan M Davis via Digitalmars-d wrote:

 Hmmm. That would make abstract inconsistent with other attributes on a
 class, but after messing around with it a bit, it looks like the only effect
 that marking a class with abstract has is how many times you get a linker
 error about the member function not being defined. So, it looks like a I did
 indeed remember incorrectly (or just misunderstood), but the behavior that
 we do get with regards to putting abstract on the class is rather weird as
 well as inconsistent with what happens with other attributes on a class. But
 on reflection, that's probably because you wouldn't want to mark a function
 with a body as abstract, meaning that marking abstract on a class would
 either have to be ignored as it seems to be, or it would have to be ignored
 on any function that has a body, which would probably cause problems with
 .di files. So, I stand corrected.
The behavior actually makes sense. If you mark the whole class as "abstract", you cannot create instances of the class. If you mark a method "abstract" it will force any sub classes to override that method. Since a sub class has to override the method it doesn't make sense to allow to create instances of the class, implicitly making the whole class abstract. -- /Jacob Carlborg
May 31 2017
prev sibling parent Moritz Maxeiner <moritz ucworks.org> writes:
On Tuesday, 30 May 2017 at 22:13:54 UTC, Wulfklaue wrote:
 I am just looking up abstract class information and i notice 
 that there is no information on dlang.org/spec
They are defined where the abstract attribute is defined[1], but there is not much to them. [1] http://dlang.org/spec/attribute.html#abstract
May 30 2017