Haxe's Array Type

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.


var ar:Array<Int> = [];
someMethodToRetrieveArrayType(ar); //returns Int

i am searching for a method that will take “ar” as a paramer and return “Int”.

is there any way to achieve this?

You could do Type.typeof(ar[0]); which returns a ValueType http://api.haxe.org/ValueType.html

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);

This takes into consideration that i have at least 1 object inside the array… what if the array is empty?

I would say, probably the same way you would do with java:


as Haxe Class also seems to have the getComponentType() method

Never tested it on haxe though :wink:

EDIT: indeed the getComponentType() seems to be only available for java target. So I don’t know :wink: . 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…

1 Like

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?

I can’t see that anywhere on the Haxe API.

1 Like

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) :wink:
My bad

Thanks anyways, this seems like a problematic issue for now…

I don’t think this is possible at runtime because the type information won’t exist in most targets.

What is the reason for wanting to know this? It’s certainly possible in macros, but it also be possible to resolve as a matter of architecture :slight_smile:

1 Like

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.


 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]

Why not use a vector instead of an array if you want to have a specific length then?

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 :slight_smile:

Maybe you could do a multi-type abstract https://github.com/HaxeFoundation/haxe/blob/development/std/Map.hx

You’d have to make several classes, but if you share code between them it shouldn’t be too much.

And it looks like it’s what you’re looking for.

@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?

Ok so this solution isn’t that easy to make but it works.

http://try.haxe.org/#4A2fd (for some reason the tryhaxe compiler require a [] line 24)

1 Like

Thanks for this!
Have to add support for other Array methods (like push, pop, shift etc…)
But i think this is very handy.

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 :slight_smile:


  1. A non related question: What is the benefit of using openfl.Vector over haxe.ds.Vector ?

  2. 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.

I thought abstract was the only way to add array access?

Maybe it is an abstract and that’s why I see no difference in code?
Sorry It should be

class MyArray < T > implements ArrayAccess < T >