On Sunday, 8 January 2023 at 12:39:37 UTC, thebluepandabear wrote:
...
The `foreach` worked for that case since `bark` is a method of `IDog`. `update` is not so a conversion to `Button[]` is needed.


In that case, you could do a casting like this:

import std.stdio, std.conv;

interface IDog {
    void bark();
}

class Dog: IDog{
    string s;
    this(string _s){s = _s;}
    void bark(){writeln(s);}
    void update(string _s){ s ~= " - " ~ _s; }
}

class List{
    IDog[] dogs;
    void add(T)(T d){ dogs ~= d;  }
}

void main(){
    auto d1 = new Dog("meaw!");
    auto d2 = new Dog("wof!");
    auto l = new List();
    l.add(d1);
    l.add(d2);

foreach(i,d; l.dogs){ // I'm using 'i' just to show each update.
        (cast(Dog)d).update(to!string(i));
        d.bark();
    }
}

Prints:

meaw! - 0
wof! - 1

Matheus.

Reply via email to