Skip to content
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

What is the functionality of sol::meta_method::call_construct? #1482

Open
cclark25 opened this issue Apr 15, 2023 · 1 comment
Open

What is the functionality of sol::meta_method::call_construct? #1482

cclark25 opened this issue Apr 15, 2023 · 1 comment

Comments

@cclark25
Copy link

cclark25 commented Apr 15, 2023

There seems to be no documentation on this specific metamethod.
My assumption based on the context is that this metamethod acts the same way as sol:call_constructor when creating a new usertype, which is to allow the syntax local val = new ClassType(), but it does not seem to work that way.

The sol2 docs say that this metamethod is sol2 specific, but doesn't elaborate any more on it. What is the intended functionality of this metamethod?

@EvanBalster
Copy link

EvanBalster commented May 27, 2024

It allows for constructor calls that look more like C++ — eg. vec3(a,b,c) instad of vec3.new(a,b,c).

From https://sol2.readthedocs.io/en/latest/api/usertype.html ...

  • sol::call_constructor, {valid constructor / initializer / factory}
    • The purpose of this is to enable the syntax local v = my_class( 24 ) and have that call a constructor; it has no other purpose
    • This is compatible with luabind, kaguya and other Lua library syntaxes and looks similar to C++ syntax, but the general consensus in Programming with Lua and other places is to use a function named new
    • Note that with the sol::call_constructor key, a construct type above must be specified. A free function without it will pass in the metatable describing this object as the first argument without that distinction, which can cause strange runtime errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants