how o use haxe.Constraints.Constructible

350 views
Skip to first unread message

Mathieu Anthoine

unread,
Jul 3, 2017, 3:57:29 AM7/3/17
to Haxe
Hi,

I try to create a generic class like

class A<T> {
    
    public var a:T;
    
    function new () {
        a= new T();
    }
}

But have the "A.T does not have a constructor" error.
So I try to use the Constructible typedef  but it is depreciated and replace by haxe.Constraints.Constructible
The problem is that I found no example with haxe.Constraints.Constructible and don't understand how it works to have a similar example.

Thank you for your help


Dan Korostelev

unread,
Jul 3, 2017, 5:27:31 AM7/3/17
to Haxe
This can only work with a @:generic class, because normally, type parameters are erased, so it can't generate a new call for a specific type parameter. See http://haxe.org/manual/type-system-generic-type-parameter-construction.html.

About haxe.Constraints.Constructible: as said in the API docs (http://api.haxe.org/haxe/Constructible.html), the constructor must unify with Constructible's T, so it should be a function type describing constructor signature.

Anyway, here's an example:

https://try.haxe.org/#7ef12

понедельник, 3 июля 2017 г., 10:57:29 UTC+3 пользователь Mathieu Anthoine написал:

Mathieu Anthoine

unread,
Jul 3, 2017, 5:29:20 AM7/3/17
to Haxe
Thank you Dan !

Mathieu Anthoine

unread,
Jul 3, 2017, 7:16:00 AM7/3/17
to Haxe
Dan,

Thank you for clarifications about Constructible.
Is there a way to make this case works ? 

@:generic class A<T:(String,Float)> {
var a:T;

public function new(s:Dynamic) {
a = new T(s);
}
}

class Test {
public function new(s) trace(s);

static function main() {
var a = new A<String>("hi");
        var b = new A<Float>(10.23);
}
}


Dan Korostelev

unread,
Jul 3, 2017, 8:35:04 AM7/3/17
to Haxe
Not sure what do you mean by this. A type cannot be both String and Float at the same time, and you can't construct them with `new` either.

понедельник, 3 июля 2017 г., 14:16:00 UTC+3 пользователь Mathieu Anthoine написал:

Mathieu Anthoine

unread,
Jul 3, 2017, 11:05:00 AM7/3/17
to Haxe
The idea is not to have String and Float at the same time but String or Float, like Map objects.

Chii Chan

unread,
Jul 4, 2017, 8:49:11 AM7/4/17
to Haxe
actually, a much more interesting case is whether this is possible in haxe ( see https://try.haxe.org/#4313b for a failing compile):

import haxe.Constraints.Constructible;
@:generic class A<P:Constructible<Void->Void>, T:Constructible<P->Void>> {
var a:T;
public function new() {
a = new T(new P());
}
}
class ParamA {
public var paramA = "ParamA";
public function new() trace("Constructing ParamA");
public function toString() return paramA;
}

class ParamB {
public var paramB = "ParamB";
public function new() trace("Constructing ParamB");
public function toString() return paramB;
}
@:generic class MyA<PA:Constructible<Void->Void>> {
public function new() trace(new PA() + " from MyA");
}
@:generic class MyB<PB:Constructible<Void->Void>> {
public function new() trace(new PB() + " from MyB");
}
class Test {
static function main() {
var a = new A<ParamA, MyA<ParamA>>();
var b = new A<ParamB, MyB<ParamB>>();
var c = new A<ParamB, MyA<ParamB>>();
var d = new A<ParamA, MyB<ParamA>>();
}
}

Reply all
Reply to author
Forward
0 new messages