【问题标题】:Haxe - how can I pass generic class to parameterHaxe - 如何将泛型类传递给参数
【发布时间】:2016-04-01 05:34:44
【问题描述】:
function test(cls:Class<Dynamic>) {}

test(Array<Float>); // Unexpected )

我必须通过 Array&lt;Float&gt; 作为类类型。但是,这失败了:

意外)

我怎样才能让它工作?

【问题讨论】:

    标签: generics parameters haxe


    【解决方案1】:

    这是不可能的,类似于在调用函数时无法显式指定函数的类型参数 (see here)。你可以直接传递Array

    test(Array);
    

    编译它的一种方法是使用typedef

    typedef FloatArray = Array<Float>;
    
    test(FloatArray);
    

    但是,Class&lt;T&gt; 并不真正关心Array 的类型参数,所以这样做没有意义:

    typedef FloatArray = Array<Float>;
    typedef IntArray = Array<Int>;
    
    trace(FloatArray == IntArray); // true
    

    【讨论】:

      【解决方案2】:

      试试这个。

      @:generic
      private function test<T>(type:Class<T>) { 
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-11
        • 2022-11-28
        • 2010-11-15
        • 1970-01-01
        • 1970-01-01
        • 2022-11-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多