www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How polymorphism work in D?

reply OiseuKodeur <oiseaukodeur gmail.com> writes:
I have this

```
import std.stdio : writeln;

abstract class Foo { }

class Bar : Foo
{
     float value;

     this(float t_value) { value = t_value; }
}

class Baz : Foo
{
     string name;

     this(string t_name) { name = t_name; }
}

void main()
{
     Foo foo = new Bar(10);

     if (/* typeof foo == Bar */)
         foo.value.writeln;
     else if (/* typeof foo == Baz */)
         foo.name.writeln;

     foo.writeln;
}
```

I don't understand how i can differentiate between Bar and Baz 
and to get their attributes
Nov 05 2019
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 06/11/2019 6:43 PM, OiseuKodeur wrote:
 I have this
 
 ```
 import std.stdio : writeln;
 
 abstract class Foo { }
 
 class Bar : Foo
 {
      float value;
 
      this(float t_value) { value = t_value; }
 }
 
 class Baz : Foo
 {
      string name;
 
      this(string t_name) { name = t_name; }
 }
 
 void main()
 {
      Foo foo = new Bar(10);
 
      if (/* typeof foo == Bar */)
if (Bar bar = cast(Bar)foo) bar.value.writeln;
          foo.value.writeln;
      else if (/* typeof foo == Baz */)
else if (Baz baz = cast(Baz)foo) baz.name.writeln;
          foo.name.writeln;
 
      foo.writeln;
 }
 ```
 
 I don't understand how i can differentiate between Bar and Baz and to 
 get their attributes
Nov 05 2019
parent reply OiseuKodeur <oiseaukodeur gmail.com> writes:
On Wednesday, 6 November 2019 at 06:05:25 UTC, rikki cattermole 
wrote:
 On 06/11/2019 6:43 PM, OiseuKodeur wrote:
 I have this
 
 ```
 import std.stdio : writeln;
 
 abstract class Foo { }
 
 class Bar : Foo
 {
      float value;
 
      this(float t_value) { value = t_value; }
 }
 
 class Baz : Foo
 {
      string name;
 
      this(string t_name) { name = t_name; }
 }
 
 void main()
 {
      Foo foo = new Bar(10);
 
      if (/* typeof foo == Bar */)
if (Bar bar = cast(Bar)foo) bar.value.writeln;
          foo.value.writeln;
      else if (/* typeof foo == Baz */)
else if (Baz baz = cast(Baz)foo) baz.name.writeln;
          foo.name.writeln;
 
      foo.writeln;
 }
 ```
 
 I don't understand how i can differentiate between Bar and Baz 
 and to get their attributes
thanks a lot
Nov 05 2019
parent ShadoLight <ettienne.gilbert gmail.com> writes:
On Wednesday, 6 November 2019 at 06:27:32 UTC, OiseuKodeur wrote:
 On Wednesday, 6 November 2019 at 06:05:25 UTC, rikki cattermole 
 wrote:
 On 06/11/2019 6:43 PM, OiseuKodeur wrote:
 I have this
 
[snip] Rikki's answer is the direct answer to your question since you already had the if(..) statements coded in your main function, but your code does not really exploit the main abstraction advantage that polymorphism offers. The typical use of polymorphism in terms of your example would be where you don't want to mess with the interior details of derived classes in your main function - you want to code your logic in your main function just keeping the 'interface' as defined by Foo in your head. Something like this: abstract class Foo { void writeProp(); } class Bar : Foo { float value; this(float t_value) { value = t_value; } override void writeProp() {value.writeln;} } class Baz : Foo { string name; this(string t_name) { name = t_name; } override void writeProp() {name.writeln;} } void main() { Foo foo = new Bar(10); foo.writeProp; foo.writeln; } The idea is that you can separate Baz and Bar "out of sight" (in a library for example) and write your main logic i.t.o. only Foo's. The advantage will become apparent if you want to add another class, say Boo : Foo. In your case you would need to add another else if(..) clause to your main function in addition to adding the class itself. In the above case you only need to add the class - you don't need to touch the main function.
Nov 06 2019