sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

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

Open cclark25 opened this issue 1 year ago • 1 comments

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?

cclark25 avatar Apr 15 '23 22:04 cclark25

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.

EvanBalster avatar May 27 '24 00:05 EvanBalster