Array access for jacascript externs

67 views
Skip to first unread message

Nathan Hüsken

unread,
Nov 11, 2015, 9:17:31 AM11/11/15
to HaXe Mailinglist
Hey,

I am trying to write externs for js-xlsx
(https://github.com/SheetJS/js-xlsx).

I need to write code like this:

var worksheet : Worksheet = workbook.Sheets['somesheetname'];

so, array access with a string. But how can I write the extern so I can
do array access with strings?

I tried:

extern class Workbook {
public var Sheets:Map<String,Worksheet>;
}

but I get errors like this one:

No @:arrayAccess function accepts argument of Int

Thanks!
Nathan

Dan Korostelev

unread,
Nov 11, 2015, 9:24:41 AM11/11/15
to Haxe
For such an extern where array-access is used in JavaScript API, you most probably want to use haxe.DynamicAccess instead of Map, since Map is a Haxe-implemented class and it doesn't map to plain javascript objects, unlike haxe.DynamicAccess.
Also, the `No @:arrayAccess function accepts argument of Int` means that you try to pass integer key when String (or other) type is expected.

среда, 11 ноября 2015 г., 17:17:31 UTC+3 пользователь Nathan Hüsken написал:

Nathan Hüsken

unread,
Nov 11, 2015, 9:58:34 AM11/11/15
to haxe...@googlegroups.com
Hey,

Thanks that works! I have a follow up question. What do I do, when
direct array access is possible? Like this:

var cell : Cell = sheet['cellName'];

where "sheet" is of type extern class Sheet?

Can I somehow derive from haxe.DynamicAccess[Cell]? I tried that, but it
did not work.

I also tried:

extern class Sheet {
@:arrayAccess
public function get(c : String) : Cell;
@:arrayAcess
public function set(c : String, c : Cell) : Cell;
}

but it also does not work.

Thanks!
Nathan

tom rhodes

unread,
Mar 8, 2017, 10:25:21 AM3/8/17
to haxe...@googlegroups.com
Digging up an old post here :)

@Nathan, did you get any working externs done? I need to do the exact same thing and have run into the exact same issues.

For Sheet I have...

package;

extern class Sheet
{
@arrayAccess
public function get(key:String):Dynamic;
@arrayAccess
public function set(key:String, value:Dynamic):Dynamic;
function getCell(address:Dynamic):Dynamic;

}

But I'm getting errors like "Array Access is not allowed on Sheet"...

--
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.
Reply all
Reply to author
Forward
0 new messages