You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some programming languages offer the possibility of out-parameters, aka where the user can pass in arguments as reference to a function, inside the function the value gets modified via reference and once the function returns the value stays modified.
I can modify sol::objects content if they are already of the correct type, as I can just get an already correctly typed reference to their underlying value. however, what if the passed-in sol::object is of a different type, is it possible to replace the referred-to Lua value with a new object?
localu=MyUserType()
localn=nillocalv=42DoSomething(u)
DoSomething(n)
DoSomething(v)
--all three values should now be of type MyUserTypeu:UserTypeFunc()
n:UserTypeFunc()
v:UserTypeFunc()
voidDoSomething(sol::variadic_args args)
{
sol::object obj = args[0];
if(obj.is<MyUserType>())
{
MyUserType& data = obj.as<MyUserType&>();
//modifyl MyUserType data//...//data obj refers to is now modified, as expected with an out-parameter, no issues here
}
else
{
// How to replace the type and data of whatever obj refers to with a new instance of MyUsertype?//for example, what if obj is referring to a number or nil, is it possible to replace that with a new //instance of MyUserType? I can't just do
obj = sol::make_object<MyUserType>(...)
//as it would only change what the obj in this scope would refer to, not what the input parameter //in args or the variable in Lua refers to
}
}
I know I can always use sol::variadic_results to return multiple values and do
localv=42v=DoSomething(v)
-- v is now userdata
sol::variadic_results DoSomething(sol::variadic_args args)
{
sol::variadic_results results{};
sol::object obj = args[0];
if(obj.is<MyUserType>())
{
// do stuff with userdata and return it
results.push_back(obj);
}
else
{
sol::object newUserData = ...
results.push_back(newUserData);
}
return results;
}
but I was wondering whether it's possible to mimic out-parameters somehow, as it would be more consistent with what some people are used to with other languages.
By the way, thank you for this fantastic work. I have been using sol2 for years and I am very happy with it!
The text was updated successfully, but these errors were encountered:
Some programming languages offer the possibility of out-parameters, aka where the user can pass in arguments as reference to a function, inside the function the value gets modified via reference and once the function returns the value stays modified.
I can modify sol::objects content if they are already of the correct type, as I can just get an already correctly typed reference to their underlying value. however, what if the passed-in sol::object is of a different type, is it possible to replace the referred-to Lua value with a new object?
I know I can always use sol::variadic_results to return multiple values and do
but I was wondering whether it's possible to mimic out-parameters somehow, as it would be more consistent with what some people are used to with other languages.
By the way, thank you for this fantastic work. I have been using sol2 for years and I am very happy with it!
The text was updated successfully, but these errors were encountered: