I've got a function that either can take two parameter, or calls another function to retreive these values(semi-defaults in that case).
Let's say the first function looks like this:
-- the actuall issue function:
function foo( param_1, param_2 )
local bar_1, bar_2 = param_1, param_2 or getBar()
-- make funny stuff with parameters
return funnyStuffMadeWithParameters
end
function getBar()
-- some magic to get bar.
return wx , yz
end
In this code, if no parameters are give, bar_2 would become wx and bar_1 would stay nil. I know why this happens, but I don't know how to express this conditional assignemt in a way that works.
I could do:
local bar_1 = getBar()
local _,bar_2 = getBar()
But I want to avoid multiple function calls. Also
if not bar_1 or not bar_2 then
bar_1, bar_2 = getBar()
end
Is not legit, due there are 4 possibilities not only two:
bar_1 == nil and bar_2 == nil
bar_1 == nil and bar_2 has value
bar_1 has value and bar_2 is nil
bar_1 has value and bar_2 has value
In each case, I only want to asign the default to the missing value, not both if one already has one.
My first though was something like:
bar_1, bar_2 = (param_1 or getBar() ), (param_2 or _,getBar() )
But this is no legit syntax.
EDIT: I could do:
def_bar_1, def_bar_2 = getBar()
bar_1 = param_1 or def_bar_1
bar_2 = param_2 or def_bar_2
But this could be an unnecessary function call.
If the
getBar
function call is expensive and should be avoided whenever possible then it's necessary to be explicit: