Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8171859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:35:44+00:00 2026-06-06T21:35:44+00:00

Instead of using long lists of arguments in my function definitions, I prefer to

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:35:45+00:00Added an answer on June 6, 2026 at 9:35 pm

    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. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When writing a CRUD MVC application, would you suggest using arrays instead of long
Instead of using AND in a long If statement, I'm using InStr to match
Instead using the google search appliance crawler for index content, im using a query
Instead using of: if ( ! $('#XX').is(':visible) ) Is there a value called invisible
Instead of using an interface like this: public interface IStartable { void Start(); void
Instead of using an external web-based Mercurial host, I want to set one up
instead of using getParameterByName('Field', PostData) (PostData == $('form').serialize();) I would like to write PostData.Field,
I am using jQuery with ASP.NET in a project. Instead of using ASP.NET Ajax,
Can I authenticate with just Google account username and password instead of using OAuth?
I've recently been experimenting with extending collection objects instead of using composition for my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.