Sorry, this question was probably asked and discussed before, but I'm in the middle of work atm and can't find the issue.
So consider the following code:
extern class Vector<T> {
static function ofArray<T>(v:Array<T>):Vector<T>;
}
class Base {}
class Child extends Base {
public function new() {}
}
class Main {
static function main() {
var v:Vector<Base> = Vector.ofArray([new Child()]);
}
}
this currently gives:
Main.hx:13: characters 3-54 : error: Child should be Base
Main.hx:13: characters 3-54 : have: Vector<Child>
Main.hx:13: characters 3-54 : want: Vector<Base>
which is understandable, as ofArray call receives [new Child()] with an expected type of just Array<T>, so it binds T to Child and returns Vector<Child>.
My question is: is it possible (and safe) to actually propagate the expected type through type parameters so the example above compiles?
Sorry, this question was probably asked and discussed before, but I'm in the middle of work atm and can't find the issue.
So consider the following code:
this currently gives:
which is understandable, as
ofArraycall receives[new Child()]with an expected type of justArray<T>, so it bindsTtoChildand returnsVector<Child>.My question is: is it possible (and safe) to actually propagate the expected type through type parameters so the example above compiles?