Constructor Haxe中泛型类型参数的构造

Constructor Haxe中泛型类型参数的构造,constructor,haxe,generic-type-argument,Constructor,Haxe,Generic Type Argument,我正在尝试基于函数类型参数实例化一个类。 虽然政府说这是可能的,但我不能让它工作 考虑以下代码: // Dialog base class // Every dialog in my application will derive from this class Dialog { public function new() { // do some stuff here } } // One of the possible dialogs in the

我正在尝试基于函数类型参数实例化一个类。
虽然政府说这是可能的,但我不能让它工作

考虑以下代码:

// Dialog base class
// Every dialog in my application will derive from this
class Dialog
{
    public function new()
    {
        // do some stuff here
    }
}

// One of the possible dialogs in the application
// Extends Dialog
class TestDialog extends Dialog
{
    public function new()
    {
        super();
        // do some more stuff
    }
}

// A simple class that tries to instantiate a specialized dialog, like TestDialog 
class SomeAppClass
{
    public function new() 
    {
        var instance = create(TestDialog);
    }

    @:generic
    function create<T:Dialog>(type:Class<T>):T
    {
        return new T();
    }
}
//对话框基类
//我的应用程序中的每个对话框都将由此派生
类对话框
{
公共职能(新增)
{
//在这里做些事情
}
}
//应用程序中可能出现的对话框之一
//扩展对话框
类TestDialog扩展对话框
{
公共职能(新增)
{
超级();
//多做点事
}
}
//一个简单的类,它试图实例化一个特殊的对话框,比如TestDialog
类SomeAppClass
{
公共职能(新增)
{
var instance=create(TestDialog);
}
@:通用
函数创建(类型:类):T
{
返回新的T();
}
}
这不适用于以下错误:
create.T没有构造函数


显然,我做错了什么,但是什么呢?

SpecialDialog
可能有一个不同于
对话框的构造函数。
因此,您必须约束它,然后还要约束到
对话框

包;
typedef可构造={
公共功能新():无效;
}
//对话框基类
//我的应用程序中的每个对话框都将由此派生
类对话框
{
公共职能(新增)
{
跟踪(“对话”);
}
}
类SuperDialog扩展对话框
{
公共职能(新增)
{
超级();
跟踪(“超级对话框”);
}
}
//一个简单的类,它试图实例化一个特殊的对话框,比如TestDialog
类SomeAppClass
{
公共职能(新增)
{
变量对话框=创建(对话框);
var superDialog=创建(superDialog);
}
@:通用
公共静态函数create(类型:Class):T
{
返回新的T();
}
}
课堂测试{
静态公共函数main(){
新的SomeAppClass();
}
}

这是否意味着我必须有一个typedef作为约束?一个typedef告诉编译器你将使用的构造函数的结构。我完全不明白为什么我需要这个typedef。。。一个类(IMHO)应该足以告诉编译器什么是什么。无论如何,谢谢你。
package;


typedef Constructible = {
  public function new():Void;
}


// Dialog base class
// Every dialog in my application will derive from this
class Dialog
{
    public function new()
    {
        trace("dialog");
    }
}


class SuperDialog extends Dialog
{
    public function new()
    {
        super();
        trace("super dialog");
    }
}

// A simple class that tries to instantiate a specialized dialog, like TestDialog 

class SomeAppClass
{
    public function new() 
    {
        var dialog = create(Dialog);
        var superDialog = create(SuperDialog);
    }

    @:generic
    public static function create<T:(Constructible,Dialog)>(type:Class<T>):T
    {
        return new T();
    }
}

class Test {
  static public function main() {
    new SomeAppClass();
  }
}