ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.18k stars 515 forks source link

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

Open cclark25 opened 1 year ago

cclark25 commented 1 year ago

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 commented 4 months ago

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.