Generics in a method, throws: "<Unknown<0>> should be XXX"

I have a question about generics. I’m posting the relevant parts of my code here. I appreciate if you can guide me if what I need is actually possible to achieve.

I’m using Constructible<Void->Void> because down the road (inside Room.hx) I need to instantiate the State class.

// Main.hx
var room: Room<State> = client.join("state_handler");
// Client.hx
@:generic
public function join<T:Constructible<Void->Void>>(roomName: String, ?options: Map<String, Dynamic>): Room<T> {
    return new Room<T>(roomName, options);
}
// Room.hx
@:generic
class Room<T:Constructible<Void->Void>> implements IRoom {
// ...
}

Here’s the error this throws me:

Source/Main.hx:24: characters 15-48 : io.colyseus.Room<Unknown<0>> should be io.colyseus.Room_State
Source/Main.hx:24: characters 3-48 : io.colyseus.Room<haxe.Constructible<Void -> Void>> should be io.colyseus.Room_State

I’m using Haxe 4.0.0-rc.2

Thanks in advance!

BTW, the State.hx looks like this:

class State extends Schema {
	public function new () {
		super();
	}
}

Here’s a Try-Haxe link easier to play with: http://try-haxe.mrcdk.com/#D03bf

Appreciate if you can give me some directions if that’s possible to achieve.

I think the problem is that you don’t pass any argument in join method that should identity T class.
Fix: http://try-haxe.mrcdk.com/#19D03

But I found an another strange behavior:

var secondRoom = client.join(State, "other"); // ok

but

var secondRoom:Room<State> = client.join(State, "other"); // error Room<State> should be Room_State

1 Like

Thanks a lot for the reply @deepcake, I wonder if this error you mentioned is expected or a bug in the language?