Generics 正确调用haxe3中的泛型函数

Generics 正确调用haxe3中的泛型函数,generics,syntax,haxe,Generics,Syntax,Haxe,下面的函数没有任何用处,但演示了我尝试使用的语法 @:generic public static function newPoint<T: Dynamic>(point: Point<T> = null): Point<T> { if (point == null) point = new Point<T>(); return point; } @:通用公共静态函数newPoint(point:point=null

下面的函数没有任何用处,但演示了我尝试使用的语法

@:generic public static function newPoint<T: Dynamic>(point: Point<T> = null): Point<T>
{
    if (point == null)
        point = new Point<T>();
    return point;
}
@:通用公共静态函数newPoint(point:point=null):point
{
如果(点==null)
点=新点();
返回点;
}
如何确定/通过T<代码>变量pt:Point=newPoint()给我一个错误,与
var pt:Point=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];
}
@:generic静态私有函数randomElement(数组:数组,usedIndices:Map=null):T
{
var-ix:Int;
如果(usedIndices!=null)
{
做{
ix=Math.floor(Math.random()*array.length);
}while(usedices.exists(ix)=true);
使用骰子集(ix,0);
}
否则{
ix=Math.floor(Math.random()*array.length);
}
返回数组[ix];
}
现在这对我来说完美无瑕:

var elems: Array<Int> = [2, 3, 8, 7, 11, 16];
var elem: Int = randomElement(elems);
varelems:Array=[2,3,8,7,11,16];
变量元素:Int=随机元素(元素);

所以它看起来像是自动正确地确定的,尽管我不知道具体是如何确定的。这是有道理的,但确切的规则是什么?

我不确定这是一个真正的问题,还是仅仅为了测试泛型函数是如何工作的

在你的例子中,假设有点奇怪。使用Point进行图像处理,这在泛型类中应该是可能的。
point.x=5
将如何工作?那个么,为什么你们希望点是通用的,你们想做什么呢

为了更好地理解泛型函数如何工作,我发现Lambda类是一个有用的参考。

你好,马克。也许这个例子并不清楚;更准确地说,我有一组类,Point2i、Point2f、Point3i、Point3f等等。现在,对于相同的数据类型(例如,Point2i和Point3i都使用整数组件),创建类层次结构很容易,但是我想创建一个具有泛型数据类型的类。碰巧我复制了一个静态公共方法,在这里我经历了不寻常的行为,它本身看起来有点奇怪。然而,我真正想知道的是调用语法。我对示例进行了编辑,使其看起来更合理。