The following function does nothing useful, but illustrates the syntax I am trying to use.
@:generic public static function newPoint<T: Dynamic>(point: Point<T> = null): Point<T>
{
if (point == null)
point = new Point<T>();
return point;
}
How is T determined/passed? var pt: Point<Int> = newPoint<Int>(); gives me an error, same with var pt: Point<Int> = newPoint();
So what is the correct way of calling such a generic function? Hours of searching for an example have given me nothing - Haxe really is a black box at times.
Here is another example:
@: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];
}
Now this works flawlessly for me:
var elems: Array<Int> = [2, 3, 8, 7, 11, 16];
var elem: Int = randomElement(elems);
So it looks like the <T> is determined automatically properly, although I don't know how exactly. It can make sense, but what are the precise rules?