Lua defining a function with optional parameters (SOLVED)

Hello, I need a minute of Lua help. What is the syntax for optional parameters or default values. I want something equivalent to this:

function(--[[required]]var1, --[[optional]]var2,  --[[optional]]var3)
    if not var2 then
       var2 = 6--default value
    end
    if not var3 then
       var3 = 0 --default value
    end
    --Do something with the inputs
end

Is there anything more succinct than what I have there? Optional arguments or multiple function definitions seem to be a built-in feature with most of the languages I have worked with in the past, but I can’t seem to find an equivalent for Lua. Maybe I am just looking in the wrong places! :confused: Thanks!

As far as I know, there is no syntax for it, but there might very well be conventions (maybe the ones you are using?) in either naming or luadoc or similar to denote this (@britzl?). To check for optionals, I think ‘or’ is commonly used:

function(--[[required]]var1, --[[optional]]var2,  --[[optional]]var3)
    var2 = var2 or 6 --default value
    var3 = var3 or 0 --default value
    --Do something with the inputs
end
3 Likes

The or syntax doesn’t seem too cumbersome; I’ll try it out. Thanks for the fast response!

1 Like

Like @Ragnar_Svensson writes there is no mechanism in the language itself to indicate that a function parameter is optional, but there are plenty of examples in the Lua standard API where optional parameters exist and where the behaviour of a function depends on the number of arguments provided:

table.concat (table [, sep [, i [, j]]])
table.insert (table, [pos,] value)
os.date ([format [, time]])

…and many more.

In your code you can assign default values using the or logical operator like in Ragnar’s example. A lot of Lua libraries use LDoc to write and automatically generate documentation, and optional arguments are documented like this:

--- Three dashes indicate the beginning of a function or field documented
-- using the LDoc format
-- @param var1 The first argument
-- @param[opt=6] var2 The second argument
-- @param[opt=0] var3 The third argument
-- @return Max The maximum value of var1 and var2
-- @return Min The minimum value of var2 and var3
function foobar(var1, var2, var3)
	var2 = var2 or 6
	var3 = var3 or 0
	return math.max(var1, var2), math.min(var2, var3)
end

And the resulting generated docs would look like:

The above example is just a subset of what can be done using LDoc.

5 Likes

Wow, thanks for the clarification! LuaDoc is intriguing, too. I don’t know if I have a use for it now, but thanks for the overview - I’ll keep it in mind! I might use the commenting conventions LDoc uses for my own modules; They remind me of python’s conventions, which I am familiar with. Do you guys on the Defold team adhere to a specific style-guide, or is there a proprietary one? Asking out of curiosity, as I personally have never been in industry and don’t know what the norm is.

Thanks again, you guys are the best!:+1:

2 Likes

When we write Lua code we use a slightly modified version of this style guide: https://github.com/Olivine-Labs/lua-style-guide

And when we design out APIs we try to follow the simple rule of “Do what Lua does” meaning that we try to follow the conventions set by the Lua standard libraries in terms of naming conventions, return values etc

4 Likes