i don’t know what to do on this
as a code block (with the flixel stuff removed as that’s not actually significant to this problem)
class Test {
var thingArrayVar:Array<IThing>;
public function doStuff() {
thingArray(new Array<ThingWithInterface>());
}
public function thingArray<T:IThing>(arr:Array<IThing>):Bool {
thingArrayVar = arr;
return arr[0].foo;
}
}
interface IThing {
public var foo:Bool;
}
class ThingWithInterfaceimplements implements IThing {
public var foo:Bool = true;
public function new() {}
}
Try this
class Test {
var thingArrayVar:Array<IThing>;
public function new() {}
public function doStuff() {
thingArray(new Array());
}
public function thingArray(arr:Array<IThing>):Bool {
thingArrayVar = arr;
return arr[0].foo;
}
}
function main() {
var test = new Test();
test.thingArray([new ThingWithInterfaceimplements()]);
test.doStuff();
}
The problem is with variance.
Here’s an example of why that won’t work:
class Test {
static function thingArray(arr:Array<IThing>) {
arr.push(new Thang());
return arr;
}
static function main() {
var arr = new Array<Thing>();
thingArray(cast arr);// let's make the compiler accept it
arr.pop().bar();// kabooom!
}
}
interface IThing {}
class Thing implements IThing {
public function bar() {}
public function new() {}
}
class Thang implements IThing {
public function new() {}
}
In your code, you could make it function thingArray<T:IThing>(arr:Array<T>)
for example. Then it would work with an array of any type that is an IThing
.