-
-
Notifications
You must be signed in to change notification settings - Fork 148
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
Allow composition of FromLuaMulti #365
Comments
Wondering, how this would work in a case when we have a backward dependency and need to go back and start converting arguments again? |
There are some other possible ambiguities as well.
calling with |
They would need to be grouped and parsed together. If they're adjacent, they can be packed into a composed I have a similar case with my skia bindings where Manual parsing would be simpler only in the case where there's a bunch of arguments between |
My current WIP trait looks like this: pub trait FromArgPack<'lua>: Sized {
fn convert(args: &mut Vec<Value<'lua>>, lua: &'lua Lua) -> Result<Self, mlua::Error>;
} This allows the implementor to return As for the error message, I think something along the lines of "expected Nth argument to be a string or table" would work fine. Languages that support function overloading provide the same level of information (as well as listing different signatures). In some cases a message like "expected a table, or inlined RGB or RGBA values" might be more appropriate. That's up to the implementer though, for your backwards dependency example a more appropriate message would be "because I would argue that an error returned from For reference, here is an example of errors I previously returned. Adding a position is basically just counting how many arguments have been previously processed. Outer scope can fill in any missing information (e.g. |
As I said earlier, here is an untested implementation that builds over existing types. It would be more convenient of course if |
I finished migration to The macro is also responsible for passing function and argument names from rust into error messages. I grant permission to this project (mlua) and its contributors to copy parts (clunky/mlua-skia) of my code (as needed), into the mlua project, under the more permissive MIT license. I might create a PR once I'm done with exams, I think mlua would benefit from a lot of linked code. |
I'm bringing a suggestion from my rlua PR (and piccolo) that proposes adding the ability to compose argument parsers. Motivation for this is simplifying function overloading from bound lua methods. There's a lot of reasons why function overloading can be useful, but the way mlua handles arguments (inherited from rlua) makes is very verbose to correctly handle it.
The idea is to allow dependents to declare a struct representing a parameter pack (e.g. Color, Position) and provide an interface to parse that parameter pack as a part of function argument list.
A simplified example is providing a function that binds to some drawing API in order to provide means of drawing a line. Here the arguments would be start position, end position and a color. In order to provide a nicer interface, the function could take parameters in following forms:
draw_line({x, y}, {x, y}, r, g, b)
draw_line(start_x, start_y, end_x, end_y, r, g, b)
draw_line(start_x, start_y, end_x, end_y, r, g, b, a)
draw_line(start_x, start_y, end_x, end_y, h, s, l, a)
draw_line(start_x, start_y, length, direction, r, g, b, a)
As visible from those 5 examples, number of combinations of different arguments can be huge for just "3" arguments and it's not practical to handle all the cases in a single method body, especially given that this is just a single function and larger bindings can have several dozen of those.
What I think is the best (DRYest) approach here, is adding a trait that allows returning converted
T
and ausize
telling the outer scope how many arguments were consumed i.e. how many arguments the outer iterator should advance by.I implemented this functionality 2/3 times for rlua already while I was discussing the API with the maintainer, so I'm not particularity eager to do it again myself. I'll probably write a trait that handles the conversion in a way I find best suitable for my use case and link it here, but feel free to modify my original PR to fit the mlua API.
The text was updated successfully, but these errors were encountered: