lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On Fri, Dec 18, 2009 at 5:30 PM, Petite Abeille <petite_abeille@mac.com> wrote:

On Dec 18, 2009, at 6:05 PM, spir wrote:

> Names arguments are very cool in some cases ;-)

Right... in Lua... named parameters would be a table:

f( { a = b, c = d, ...  } )


For which Lua also provides the handy shortcut

f{ a= 1, b = 2 }


Apart from that, if you want to use positional parameters, just check for them being nil and handling that in any way that seems sensible. The already mentioned

function f(x, y, z)
    x = x or "default"
    y = y or "..."
...
end

is pretty common. But depending on what your function is doing you might include conditional code on a parameter being set or not. Beware the boolean false, in case that is important in your code, though.

function f(x, y, z)
...
    if y then
        -- do something when y ~= nil and y ~= false
    end
    if z == nil then
        -- do something when z is not set, or set explicitly to nil
    end
...
end

Robby