Array access for classes (@:arrayAccess)

Hi there :slight_smile:

How can I possibly allow @:arrayAccess (like for instance myclass[26]) to a custom class?

I am trying to achieve this by giving my class these fields:

class MyClass {

	var innerdata : Array<T>;

	public inline function get(key:Int) {
		return innerdata.get(key);

	public inline function arrayWrite(k:Int, v:T):T { // also tried `set` as function name
		innerdata.set(k, v);
		return v;

Is @:arrayAccess only possible with abstracts? (Also the manual puts it under the category for abstracts)

(Unfortunately, I can’t use an abstract instead, because then I cannot declare member variables /variable fields :confused: )

EDIT: Oh, maybe these criteria must be met somehow manual/expression-array-access? However

class MyClass<T> extends Array<T> {...}

won’t work it seems.

“This interface should be used for externs only. Haxe does not support custom array access on classes. However, array access can be implemented for abstract types.”

Oh, okay, that explains why it won’t work here. Thank you!

You can use @:forward metadata on the abstract to forward variables/fields from the underlying type.

abstract MyAbstract(MyType) {}

class MyType {
	public var myVar:String;

With that @:forward, I think that this will work:

var myAbstract:MyAbstract = new MyType();
myAbstract.myVar = "hi";

Okay, wow, that little trick will allow me to fully build my idea! Thank you! :smiley: