HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.19k stars 656 forks source link

Allow to create Lua tables with named and numeric indexes #10985

Closed danielo515 closed 1 year ago

danielo515 commented 1 year ago

Hello. I may be opening a pull request if you like this proposal. Currently, there is one common Lua idiom that is not possible to build using regular haxe code, and that is tables that have both numeric indexed and named indexed. From the lua perspective they are not different. So something like this is some common in certain Lua communities:

{ 
"author/plugin-name",
config_option = "bla",
doStuff = true
}

This is impossible to do currently in Haxe without resorting to untyped lua blocks and string interpolation. The existing lua.Table.create already has the right signature to make this possible. It takes both an array and an object, but it decides to ignore one or the other depending which one is not null. Allowing creating this kind of tables does not require any change in syntax not even signature. All is needed is make the existing lua.Table.create build a lua table including values from both arguments if both are present.

What do you think?

sebthom commented 1 year ago

I also had difficulties working with lua.Table. I created some abstraction around it. have a look at the types LuaTable/LuaMap/LuaArray in https://github.com/vegardit/haxe-clink-externs/tree/main/src/clink/util if there is interest I could extract them into a separate haxe lib.

danielo515 commented 1 year ago

I also had difficulties working with lua.Table. I created some abstraction around it. have a look at the types LuaTable/LuaMap/LuaArray in https://github.com/vegardit/haxe-clink-externs/tree/main/src/clink/util if there is interest I could extract them into a separate haxe lib.

@sebthom it seems we all have our own set of utilities around Lua Tables 😄 . Yours in particular seems to be very well put toghether, and interesting. However, I still want to have better support for Lua tables in Haxe itself. Almost all utilities have a runtime penalty. I built a complete set of macros to avoid such penalty while keeping complete type safe code, but I am already hitting some limitations in the Haxe API, this being one of them.

Maybe, rather than putting your nice tooling in a separate library (which will be useful to many that want to stick to current haxe version) it could be a cool idea to integrate some into the Haxe Lua core?

Frixuu commented 1 year ago

I might be misunderstanding the problem here, but I tried it on both 4.2.5 and nightly:

final tbl = lua.Table.create(["foo", "bar"], {
    option: "blah",
    other: true,
});

which, in both cases, compiled down to

local tbl = ({"foo","bar",option = "blah", other = true});

so I'm not exactly sure what is being ignored in your case?

danielo515 commented 1 year ago

No, you understood the problem perfectly. That is indeed what I expect. Let me try to reproduce with my current version, which is 4.2.5

danielo515 commented 1 year ago

Ok, I tested it and works as expected. Not sure what I tried that didn't worked, but this was not it. Sorry about the inconvenience.