Frage

I have one prototype class that is desribed like:

class CEnumList<T:EnumValue> {
    ...
    public function clone():CEnumList<T> {
        var result:CEnumList<T> = new CEnumList<T>();
        ...
        return result;
    }
}

Clone method implements standard procedure of creating a full copy of the instance. And I have one inherited class:

class CElements extends CEnumList<EElements> {
    ...
}

Where EElements is enum type. But when I call somewhere:

var damage:CElements = baseDamage.clone();

I have an error "data.CEnumList should be data.char.CElements" and that's absolutely correct. But how I should inherit prototype clone() method if my inherited classes differs only by enum types? Should I create as many clone() methods as I have different classes and all these methods will just cast type of the private base class cloning?

Probably it isn't a question about haxe at all but question about application architecture.

War es hilfreich?

Lösung

If you need to return exactly CElements then yes, you will need another clone method that returns CElements.

But you may want not to extend CEnumList but to typedef it like this

typedef CElements = CEnumList<EElements>;

That should do the trick because the result will be correctly typed (as types will be identical, typedef just gives a synonym).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top