Real OOP with D

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 16 23:08:34 PDT 2015


On 08/16/2015 10:57 PM, Ozan wrote:

 > Working with objectoriented concepts results often in large trees of
 > related classes. Every instance of a class knows his methods and data.
 > An example like following would work:
 >
 > import std.stdio;
 > class Family { }

 From the way you use it below, a better name for that class would be 
FamilyMember but I get the point.

 > class Dad : Family { void greeting() { writeln("I'm dad"); } }
 > class Boy : Family { void greeting() { writeln("I'm daddy's boy"); } }
 > void main() {
 > writeln("Father and son");
 > Dad father = new Dad;
 > Family son = new Boy;
 > father.greeting;
 > son.greeting;

What you mean is that the call above causes a compilation error:

Error: no property 'greeting' for type 'deneme.Family'

 > }
 >
 > The critical point is using a variable of type Family for an instance of
 > Boy. Class Family covers the greeting method of Boy. In real OOP that
 > would not be a problem, because the access point of view starts with the
 > object.

My experience with OOP is limited to C++ and D. I think what you are 
describing is how dynamically typed languages work. It is impossible in 
compiled languages like C++ and D. When a type is used in an expression, 
the compiler ensures that the call is bound to a function.

 > In D, it starts with the class definition.

Actually, 'interface' is a better fit in most cases:

interface FamilyMember
{
     // ...
}

class Dad : FamilyMember
{
     // ...
}

 > Is there any way to get real OOP with D?
 >
 > Regards,  Ozan

Ali



More information about the Digitalmars-d-learn mailing list