-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Lua] 0-indexed arrays #39
Comments
Under what circumstances would one need to "access a classic Haxe array from Lua"? Our Haxe-generated Lua code uses native Lua tables, doesn't it? |
import lua.Table;
@:expose
class Tests {
public function new()
{
}
// Classic return, that's how we do in hsluv
public static function returnArray():Array<String>
{
return ["Hello", "World"];
}
// Using the lua.Table function instead
public static function returnTable():Table<Int, String>
{
var table = Table.create(["Hello", "World"]);
return table;
}
} -- Small file to test the exported lib
print("--- Start Tests ---")
-- The Haxe file is here compiled into test.lua
local test = require("test")
print(test.Tests.returnArray()[1])
print(test.Tests.returnTable()[1]) Yields
|
Note, the compiled function explicitely set the 0th element when just returning an Haxe array: _hx_exports["Tests"] = Tests
Tests.returnArray = function()
do return _hx_tab_array({[0]="Hello", "World" }, 2) end;
end
Tests.returnTable = function()
local table = ({"Hello","World"});
do return table end;
end
|
Ok I understand now. I assumed you were referring to https://github.com/hsluv/hsluv-lua code when you ran into the limitation. The Lua code there is a cleaned up version of the Haxe output. What you're suggesting makes sense, but I'm not sure if it's worthwhile considering that Haxe-generated Lua is very messy anyways and needs manual cleanup. The underlying problem here is using the array/list type to represent colors. The proper type for these values is a tuple/struct/class/"product type" but all of those tend to generate a lot of boilerplate when compiled with Haxe. |
Well, I didn't check hsluv-lua's codebase, I just referred to Haxe's output but yeah, if it have a cleaner api, I guess it'd be interesting to tell the users to avoid exporting to Lua as the other repo is more accessible. |
Hello.
I tried to use the lib in a simple Lua project but I quickly went against a design issue with the current API : Lua's table indexes start at 1, whereas almost the rest of the world, including Haxe, starts at 0. So a classic Haxe array will be a bit weird to access from Lua as it might induce code such as manually setting the 0th element instead of directly using an array.
This is almost an edge case, so I was wondering if wrapping or conditionnal compilation would allow us to offer to lua users a module easily useable without a weird interface.
The text was updated successfully, but these errors were encountered: