-
-
Notifications
You must be signed in to change notification settings - Fork 658
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
[Feature Request / Lua] Type for handling Lua var args "..." #7306
Comments
I will look into a method for supporting this. It will require a lot of checks and restrictions as you note. |
I wonder if it would make sense to reuse |
Yeah, I don't think overloading the |
Rest had been lightly misuseable for a long time now. you can do class Some {
public static var log:(tag:String, rest:haxe.extern.Rest<Dynamic>)->Void = Reflect.makeVarArgs(...);
} and that will work. Personal opinion is that a simple Rest (covering just array read[] and .length) should have been made accessible in non-extern types instead of adding Reflect.makeVarArgs to begin with, as all (?) targets support overloading and/or varargs natively, so the worst case scenario is having to account for rest-argument offset for targets with JS-style schema. |
Hey, I'm also interested in this. My current solution involves this macro: https://github.com/vendethiel/haxewow/blob/master/Test.hx |
Another workaround would involve using metadata on a function: @:luaVarArgs
static function foo(x:Int) {
trace(x);
trace(lua.Lib.varargs);
}
This would generate a function signature with varags, and remove the possibility that the user screws the order up (varargs must be at end - currently the extern does not throw an appropriate error if that is not the case). This wouldn't really work with closures, and the varargs would be untyped. |
On second thought, that would still require Reflect.makeVarArgs. I think we can do better. |
I now have a test branch here : https://github.com/HaxeFoundation/haxe/tree/lua_varargs I'm going with the "abuse var f = function(x:Int, y : haxe.extern.Rest<String>) {
trace(x);
trace(lua.TableTools.pack(y));
}
[...]
f(1,'hi','ho');
// 1
// {1:'hi', 2 : 'ho'} This still feels kind of off to me. I'll ask some of the other compiler folks about it and see if we can get proper support for it. |
here is my use case work around class EventManager {
var frame : Frame;
var callbacks : Map<String, Function>;
public function new() {
callbacks = new Map();
frame = wow.G.CreateFrame(FRAME);
frame.SetScript("OnEvent", untyped __lua__("function(_, e, ...) {0}.h[e](...); end", callbacks));
}
public function register(event: String, callback:Function) {
frame.RegisterEvent(event);
callbacks.set(event, callback);
}
public function unregister(event:String) {
frame.UnregisterEvent(event);
callbacks.remove(event);
}
} nicer usage class EventManager {
var frame : Frame;
var callbacks : Map<String, Function>;
public function new() {
callbacks = new Map();
frame = wow.G.CreateFrame(FRAME);
frame.SetScript("OnEvent", on_event);
}
public function register(event: String, callback:Function) {
frame.RegisterEvent(event);
callbacks.set(event, callback);
}
public function unregister(event:String) {
frame.UnregisterEvent(event);
callbacks.remove(event);
}
function on_event(_:Void, e: Event, args: Rest<Dynamic>) {
callbacks.get(e)(args...);
}
} |
@jdonaldson Maybe it's time to merge that branch since it seems nobody found a better solution in the meantime? :) Or apply that approach so some new type in the |
lua.VarArgs would be nice since there is cpp.VarArg, python.VargArgs etc. |
It looks like the hack for I'll bring this up with the rest of the team, but it touches on a number of existing requested features. I don't want to do something half-baked for Lua if we can find another more general solution for other targets. |
I am using Haxe to make World Of Warcraft addons and something that is used frequently is the Lua var args as a parameter "..." my proposal is to add a specific Type for this
In Haxe I would write
and that would compile to something like this
Limitations would be that you can only place it in a parameter list and only as the last parameter.
The text was updated successfully, but these errors were encountered: