Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LUA: Seeking efficient and error-free means of assigning default arguments

Tags:

lua

Instead of using long lists of arguments in my function definitions, I prefer to pass a few fixed parameters and a table of 'additional params' like this:

function:doit( text, params )
end

This is nice as it allows me to add new named parameters later without breaking old calls.

The problem I am experiencing occurs when I try to force default values for some of the params:

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 where I have passed in 'false' for emboss:

doit( "Test text", { fontSize = 32, emboss = false } )

The above code will result in emboss being set to true when I really wanted false.

To be clear, what I want is for the first non-NIL value to be assigned to emboss, instead I'm getting a first non-false and non-NIL.

To combat this problem I wrote a small piece of code to find the first non-NIL value in a table and to return that:

function firstNotNil( ... )
   for i = 1, #arg do
      local theArg = arg[i]
      if(theArg ~= nil) then return theArg end
   end
   return nil
end

Using this function I would re-write the emboss assignment as follows:

   local emboss   = firstNotNil(params.emboss, true)

Now, this certainly works, but it seems so inefficient and over the top. I am hoping there is a more compact way of doing this.

Please note: I found this ruby construct which looked promising and I am hoping lua has something like it:

[c,b,a].detect { |i| i > 0 } -- Assign first non-zero in order: c,b,a
like image 584
Ed Maurina Avatar asked Jul 02 '12 17:07

Ed Maurina


2 Answers

Lua's relational operators evaluate to the value of one of the operands (i.e. the value is not coerced to boolean) so you can get the equivalent of C's ternary operator by saying a and b or c. In your case, you want to use a if it's not nil and b otherwise, so a == nil and b or a:

local emboss = (params.emboss == nil) and true or params.emboss

Not as pretty as before, but you'd only need to do it for boolean parameters.


[snip - Lua code]

Now, this certainly works, but it seems so inefficient and over the top.

Please note: I found this ruby construct which looked promising and I am hoping lua has something like it:

[c,b,a].detect { |i| i > 0 } -- Assign first non-zero in order: c,b,a

Your Lua function is no more over-the-top or inefficient. The Ruby construct is more succinct, in terms of source text, but the semantics are not really different from firstNotNil(c,b,a). Both constructs end up creating a list object, initialize it with a set of values, running that through a function that searches the list linearly.

In Lua you could skip the creation of the list object by using vararg expression with select:

function firstNotNil(...)
   for i = 1, select('#',...) do
      local theArg = select(i,...)
      if theArg ~= nil then return theArg end
   end
   return nil
end

I am hoping there is a more compact way of doing this.

About the only way to do that would be to shorten the function name. ;)

like image 177
Mud Avatar answered Nov 19 '22 21:11

Mud


If you really want to do it in a single line, you'll need something like this for a default value of true:

local emboss   = params.emboss or (params.emboss == nil)

It's not very readable, but it works. (params.emboss == nil) evaluates to true when params.emboss is not set (when you would need a default value), otherwise it's false. So when params.emboss is false, the statement is false, and when it's true, the statement is true (true or false = true).

For a default of false, what you tried originally would work:

local emboss   = params.emboss or false
like image 43
brando Avatar answered Nov 19 '22 23:11

brando