【发布时间】:2014-01-01 16:58:57
【问题描述】:
以下函数没有任何用处,但说明了我尝试使用的语法。
@:generic public static function newPoint<T: Dynamic>(point: Point<T> = null): Point<T>
{
if (point == null)
point = new Point<T>();
return point;
}
如何确定/通过 T? var pt: Point<Int> = newPoint<Int>(); 给我一个错误,与 var pt: Point<Int> = newPoint(); 相同
那么调用这样一个泛型函数的正确方法是什么?搜索示例数小时没有给我任何帮助 - Haxe 有时确实是一个黑匣子。
这是另一个例子:
@:generic static private function randomElement<T>(array: Array<T>, usedIndices: Map<Int, Int> = null): T
{
var ix: Int;
if (usedIndices != null)
{
do {
ix = Math.floor(Math.random() * array.length);
} while (usedIndices.exists(ix) == true);
usedIndices.set(ix, 0);
}
else {
ix = Math.floor(Math.random() * array.length);
}
return array[ix];
}
现在这对我来说完美无缺:
var elems: Array<Int> = [2, 3, 8, 7, 11, 16];
var elem: Int = randomElement(elems);
所以看起来
【问题讨论】: