-
Notifications
You must be signed in to change notification settings - Fork 40
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
Handling of LUA_TNIL and LUA_TNONE with boolean parameters #105
Comments
Why not post it in #82? One could argue that if you don't need to pass anything as parameter then it should be wrapped with |
I mainly posted a new issue because it is to report the bug that omitting lua boolean parameters causes It also appears that at least Stack::get will get a boolean value as true if it is anything other than nil or nothing or false. |
Concerning breaking changes, to me this situation appears similar to what happened with the introduction of integer types in Lua 5.3. By default, Lua 5.3 would emit a runtime error when you assigned a non-integer floating point value to an integer property. (At least, to an integer property created by LuaBridge on a C++ class.) This was a massive breaking change and a huge barrier to adoption. It is the reason that my project never progressed past 5.2 for 6 years. I recently decided to investigate the issue further and discovered that Lua offers a compile-time macro to truncate floats into integers instead of issuing runtime errors. I am advocating something similar with LuaBridge 3 and omitted booleans, because unless this issue is addressed I can't use LB3. And I really do want to use it. |
If you declare the method with bool, but call it without parameters, that should raise a lua error. We should gradually move to use optional for optional parameters. |
I don't disagree, but it is a breaking change. I think it is important not to let the perfect be the enemy of the good. |
The reason i took over luabridge3 was to make things consistent and more aligned with how modern C++ works as well, regardless of breaking changes. This code is more than 10 years old, and what was good 10 years ago, might not be so good nowadays. |
Breaking changes are a big deal. In putting all this work into LB3, surely you would prefer to see wider rather than narrow adoption. Breaking changes are a major impediment to adoption. Your statement is pretty much the definition of making the perfect be the enemy of the good: almost never a good choice. And consider that the older All that said, I think we can agree that the current behavior of sending I may be prepared to eat the breaking change for omitted boolean params. Indeed, you will find that I complained about this from the opposite side in the LB2 repo, but since no fix would ever be forthcoming, I embraced the behavior. Now I'll have to de-embrace it and go find and fix any documentation I wrote that allowed it. |
Here's a test case: class foo
{
bool val_;
public:
foo(bool param) : val_(param) {}
bool val() const { return val_; }
};
luabridge::getGlobalNamespace(L)
.beginNamespace("foobar")
.beginClass<foo>("foo")
.addConstructor<void(*)(bool)>()
.addFunction("val", &foo::val)
.endClass()
.endNamespace(); local foo = foobar.foo()
print (foo:val()) -- > prints "true" In this case |
Some design decisions in LB2 are also questionable. Breaking changes are surely annoying, but all changes come at a cost, if not, this would be LB 2.0.1 not 3.0.0. Btw, thanks for the repro case. |
If only there were a way to treat an old-style optional parameter as |
The idiomatic way to pass optionals in C++ is std::optional nowadays. |
Pushed a fix to master |
So say many C++ purists, but it will never be true. The syntax of |
they serve different purposes. |
I agree. They serve different purposes C++. The issue is, how to port a function with default arguments into Lua with LB3. What I wish for is a way we could mark the default arguments to be treated as std::optional inside LB. This would allow Lua programs to call the same way C++ functions do. I realize it can be done with proxy functions. I spent a couple of hours this morning trying to write a generic proxy function to let default arguments be optional in Lua, but even with ChatGPT helping I couldn't get it to work. I've discovered that when I ask ChatGPT to do something that isn't really possible I get wrong code from it. 🤣 |
My code is working now, so I will close this issue. We still have #82 to track the larger question. |
I usually wrap the methods i can't change with lambdas. |
Yes, what I was thinking about was whether is a way to make that wrapping process less cumbersome. So far I haven't com up with one. |
This a follow-on to issue #82.
Suppose you have this C method registered in Lua:
With LB2, any of the following calls in Lua leads to
my_method
being called witharg == false
in C++.I currently have a constructor function with a boolean parameter that is omitted by the Lua code. LB3 is calling the constructor with
true
rather thanfalse
!(!!!!)
This is perhaps the most wrong of any possible choice. I see only two possible acceptable behaviors if a boolean parameter is missing or nil:false
to the C function like LB2 does.If I were starting from scratch I might vote for option 2. But my project has been going a long time with the assumption of option 1. Many of the constructors and methods have trailing optional boolean parameters that C++ defaults to
false
. The option 1 behavior allows those trailing booleans also to be optional in Lua for free.For my project, switching to LuaBridge3 (which I would very much like to do) is contingent on no breaking changes to the many thousands of lines of code written for Lua on Finale over the last nearly 20 years. Option 2 would be an unacceptable breaking change. I'm a believer in flexibility and options, so perhaps the choice between 1 & 2 should be both well-defined and documented (which I don't think it is at the moment) and selectable by means of a compile-time macro.
The text was updated successfully, but these errors were encountered: