Re: Is it possible to store different subclasses in one array?

2020-04-14 Thread Manfred Nowak via Digitalmars-d-learn
On Monday, 13 April 2020 at 05:54:52 UTC, evilrat wrote: if (auto weapon = cast(Weapon) gi) weapon.Attack(); Does the parlor (currently illegal in Dlang) if (Weapon w := gi) w.Attack(); look nicer or even (currently legal): if (Weapon w ._= gi) w.A

Re: Is it possible to store different subclasses in one array?

2020-04-13 Thread Leonardo via Digitalmars-d-learn
On Monday, 13 April 2020 at 05:54:52 UTC, evilrat wrote: On Monday, 13 April 2020 at 04:21:48 UTC, Leonardo wrote: foreach (ref gi; GameItems) { if (gi == Weapon) gi.Attack() } How would it be? Replying myself... weapon = cast(Weapon) gi; if (weapon !is null) weapon.Attack(

Re: Is it possible to store different subclasses in one array?

2020-04-12 Thread evilrat via Digitalmars-d-learn
On Monday, 13 April 2020 at 04:21:48 UTC, Leonardo wrote: foreach (ref gi; GameItems) { if (gi == Weapon) gi.Attack() } How would it be? Replying myself... weapon = cast(Weapon) gi; if (weapon !is null) weapon.Attack() can be simplified as: if (auto weapon = cast(Weap

Re: Is it possible to store different subclasses in one array?

2020-04-12 Thread Leonardo via Digitalmars-d-learn
On Monday, 13 April 2020 at 04:15:04 UTC, Leonardo wrote: On Monday, 13 April 2020 at 01:47:11 UTC, Adam D. Ruppe wrote: On Monday, 13 April 2020 at 01:42:51 UTC, Leonardo wrote: Is it possible to store different subclasses in one array? In C#, we have this example, but how I do that in D

Re: Is it possible to store different subclasses in one array?

2020-04-12 Thread Leonardo via Digitalmars-d-learn
On Monday, 13 April 2020 at 01:47:11 UTC, Adam D. Ruppe wrote: On Monday, 13 April 2020 at 01:42:51 UTC, Leonardo wrote: Is it possible to store different subclasses in one array? In C#, we have this example, but how I do that in D? Did you try BaseItem[] GameItems; GameItems ~= new Weapon

Re: Is it possible to store different subclasses in one array?

2020-04-12 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 13 April 2020 at 01:42:51 UTC, Leonardo wrote: Is it possible to store different subclasses in one array? In C#, we have this example, but how I do that in D? Did you try BaseItem[] GameItems; GameItems ~= new Weapon(); yet?

Is it possible to store different subclasses in one array?

2020-04-12 Thread Leonardo via Digitalmars-d-learn
Is it possible to store different subclasses in one array? In C#, we have this example, but how I do that in D? public class BaseItem{ public string name = ""; } public class Weapon : BaseItem{ public int damage = 10; } public class Potion : BaseItem{