Cant add @:arrayAccess to extern JS class

47 views
Skip to first unread message

TiagoLr

unread,
Oct 3, 2015, 3:20:25 PM10/3/15
to Haxe
Hi, I already have an extern JS class with a fair amount of fields.

The javascript file uses array access to access members of that class like so:

Template['templateName'].field = .....

At the moment I'm using the following work around:

inline static function get(template:String):Template return untyped Template[template]; // TODO array access this class

It works but it would be better if i could add array access to it.
Unfortunately I'm getting compilation errors trying to do so, here is a small example : http://try.haxe.org/#8d133

Anyone can point me what I'm doing wrong? Thank you.

Andy Li

unread,
Oct 3, 2015, 5:32:21 PM10/3/15
to haxe...@googlegroups.com
@:arrayAccess is for abstracts.
Extern classes and interfaces should use `implements ArrayAccess<SomeType>` (e.g. JQuery).

Best,
Andy

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

TiagoLr

unread,
Oct 3, 2015, 6:38:01 PM10/3/15
to Haxe
Thanks, still i can't reproduce the javascript syntax used:

var myTemplate = Template['name'];


Best i could do was to reproduce the example in http://old.haxe.org/api/arrayaccess?version=6339 which is a bit different use case.

Think its possible to achieve that syntax in current haxe versions? 

Andy Li

unread,
Oct 3, 2015, 7:02:58 PM10/3/15
to haxe...@googlegroups.com
Ah, I didn't notice you want to use strings as keys, which is not supported by the `implements ArrayAccess<T>`.

It would be better to see the current definition of Template or else it is hard to come up with a good suggestion. It is not clear to me whether it is a class object or an instance or a map.  

It looks like the Template object is being used as a map. Does it has any other instance fields? If no, I suggest just type it as haxe.DynamicAccess<T> (usage example).

Best,
Andy

--

TiagoLr

unread,
Oct 3, 2015, 7:13:51 PM10/3/15
to Haxe
Yes, you can find the class API here: http://docs.meteor.com/#/full/templates_api

I implemented the class fields as being static: http://pastebin.com/GPaJhG2i , so even if i implement the DynamicObject class i would have to create an instance, besides it would add methods that do not exist.

I'm afraid there is no better solution to replace that `inline get()` , in that case its no big deal anyway.

Mark Knol

unread,
Oct 6, 2015, 2:56:35 AM10/6/15
to Haxe
What is the reason you don't use Template.name?

TiagoLr

unread,
Oct 6, 2015, 9:59:04 AM10/6/15
to Haxe
The compiler will reply: name is undefined. Or am i missing something?
Reply all
Reply to author
Forward
0 new messages