Skip to content

should expected type be propagated through type params #8186

Description

@nadako

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions