Hi everyone,
I’m trying to know the declared type of an array in runtime, couldn’t find any way to doing so…
I want to know the declared type of the array’s items.
example:
var ar:Array<Int> = [];
someMethodToRetrieveArrayType(ar); //returns Int
i am searching for a method that will take “ar” as a paramer and return “Int”.
Otherwise, if it’s a type of Class or Enum, you can get the name by using Type.getClassName(myClass); or Type.getEnumName(myEnum); which returns the name plus the package it’s in. If you just want the name (not the package) you can do:
var fullEnumName = Type.getClassName(array[0]);
var lastDotIndex = fullEnumName.lastIndexOf('.');
var enumName = fullEnumName.substring(lastDotIndex);
I would say, probably the same way you would do with java:
ar.getClass().getComponentType();
as Haxe Class also seems to have the getComponentType() method
Never tested it on haxe though
EDIT: indeed the getComponentType() seems to be only available for java target. So I don’t know . Not sure it is even possible as for some targets, the declared type of arrays is probably ignored and set to “Dynamic” so you can probably only test if there is already one object in your array as tienery proposed…
Well, doing a Type.typeof(t) (where t is an instance of Array<Int>) returns TClass because Array is a Class of a generic type.
You can do a check to see if there are any elements. Doing trace(Type.getClassName(Type.getClass(t))); is not very useful either because that returns Array, not Int, or Array of Int.
You can always make an issue on Haxe Git to propose such a feature?
Yes I just googled “haxe getComponentType” because this is the method I would use on java and found this: http://old.haxe.org/api/java/lang/class but didn’t pay attention to the fact it was “Available in java” only (thought it was the haxe Class when it actualy was direct access to the java one)
My bad
I want to create an abstract class that extends Array and i want to set length to have setter as well.
now, if the array’s length is set to a bigger value i want to put a class-dependent default value in the empty positions…
this means
if its an array of integers/floats, i want to populate it with ‘0’.
If it is Bool, than populate with false.
etc…
examples:
var ar:MyArray<Int> = [1, 2, 5];
ar.length = 5;
// ar is now [1, 2, 5, 0, 0]
var ar2:MyArray<Dynamic> = [];
ar2.length = 2;
// ar2 is now [null, null]
Oh, gotcha, yeah this was a tricky problem to solve when I was implementing openfl.Vector
Looking at the source ( https://github.com/openfl/openfl/blob/master/openfl/Vector.hx) though, I’m not sure I see handling for it. Perhaps I tried (and failed) to get this working, though now I’m sure I could do it using a macro type if Vector doesn’t do it already
@singmajesty, Could you pleas elaborate on using Macros to do that?
I havent used macros yet, so maybe some more detailed pointing to what should i read at that topic?
how hard is it to implement?
I think @:multiType was not working when I first worked on openfl.Vector, but if its stable, perhaps we could bring this functionality to Vector if it is not there already. Would Vector cover your needs? We do want it to initialize properly (differently) for different types if possible
A non related question: What is the benefit of using openfl.Vector over haxe.ds.Vector ?
A comment on the actual question:
You can also make your own array class using “class MyArray implements ArrayAccess” so you can have a Vector or Array underneath and you can decleare your own “setType” and “getType” functions if it is enough for you. I have heard that “abstract” should generate better code but I really don’t see a difference.