Instead of using long lists of arguments in the definitions of my function, I prefer to pass a few fixed parameters and a table of "additional parameters" as follows:
function:doit( text, params )
end
This is nice because it allows me to add new named parameters later without breaking old calls.
The problem I am encountering occurs when I try to force default values ββfor some parameters:
function:doit( text, params )
local font = params.font or native.systemBold
local fontSize = params.fontSize or 24
local emboss = params.emboss or true
-- ...
end
The above code works fine in all cases, except when I went "false" for embossing:
doit( "Test text", { fontSize = 32, emboss = false } )
The above code will cause the embossing to be set to true when I really need false.
, , , NIL, , -NIL.
, , , NIL, :
function firstNotNil( ... )
for i = 1,
local theArg = arg[i]
if(theArg ~= nil) then return theArg end
end
return nil
end
, :
local emboss = firstNotNil(params.emboss, true)
, , , . , .
: , , , lua - :
[c,b,a].detect { |i| i > 0 } -- Assign first non-zero in order: c,b,a