-
Notifications
You must be signed in to change notification settings - Fork 57
Add fnRegsFromStruct for comptime-generation of Lua function registrations #179
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
Conversation
|
CI failed.. Forced pushed to update. Imagine if both of these paths worked on all Lua versions lol |
|
love this!! really excited to add it :) |
| lua.registerFns("fnregs", funcs); | ||
| } else { | ||
| lua.newLib(zlua.fnRegsFromStruct(MyLib)); | ||
| lua.newLib(comptime zlua.fnRegsFromType(MyLib)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! Is moving the comptime keyword to caller site the more idiomatic way in Zig? I guess I'm used to doing it the way I did it in the original version.
For example, I see comptime used in the library, not at call time, like in the initCompTime function here:
https://ziglang.org/documentation/master/std/#src/std/static_string_map.zig
I don't have a super strong opinion about, but I guess I have a preference for version that doesn't require the callsite to mark the call comptime.
BTW, I intentionally had this working only for structs because I didn't think this sort of "get decls from a namespace" makes so much sense e.g. in enums or unions. Or it may at least be that some public funcs in an enum likely aren't zlua wrappable. In this case, requiring a struct and giving an error may guide the user of this library to the right path. OTOH, if you had some cool uses in mind, then being overly restrictive is not that great idea either!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, i think what you did was right! the reason for comptime here is actually to move it to a testable part of the API contract. sorry for also unrelatedly fiddling with the implementation a little—I liked proving to myself that my code was equivalent to yours even though it doesn't have an explicitly comptime block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you be willing to accept a PR that moves comptime back to ziglua so that users of this API don't need to explicitly add comptime? That seems to be the way it's usually done in std library at least.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would, if you insist, but I claim that you are misunderstanding what I am saying.
- the use of the
comptimekeyword in the caller here makes no difference and does not need to be done in consumer code. - the purpose of the
comptimekeyword in the test is so that in the future if the function body is altered to include runtime code, CI will fail - the code in the body of the function is executed entirely at compile time currently, even though there is no explicit
comptimeblock. please read the function carefully: the main body of work that it does constructs a[]const FnRegby modifying acomptime var. All of the code up toconst final = funcs;therefore must be run at compile time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice! Thanks for clarifying and sorry for the confusion. I had indeed misunderstood what you said before. In this case there's no need to follow up with a PR. :)
No description provided.