Page 1 of 4
Lua local question
Posted: Tue Aug 16, 2011 12:19 pm
by pancakepalace
Hi guys,
I read all the docs I could find on the Lua language, but the use of local keyword when creating default values for function parameters is still unclear to my mind.
I see this a lot. I imagine the local keyword should be used in this instance.
Code: Select all
function my_func(someArg)
local someArg = someaArg or 'someDefaultValue'
end
What about the case where the default value can also be false. This is how I code it at the moment, but I'm not sure if it's the best. I'm not using the local keyword as I am assuming it is not needed with function parameters.
Code: Select all
function my_func(someArg)
if someArg == nil then someArg = someDefaultValue end
end
Am I right that the first version should use local, but not the second?
Re: Lua local question
Posted: Tue Aug 16, 2011 12:44 pm
by kraftman
The first version shouldnt use local, since the local is already created in the function parameters
the second could be written
Code: Select all
function my_func(someArg)
someArg = someArg or someDefaultValue
end
its slightly neater, but there isn't much difference.
Re: Lua local question
Posted: Tue Aug 16, 2011 2:48 pm
by ivan
Code: Select all
function my_func(someArg)
local someArg = someaArg or 'someDefaultValue'
end
I think you mean someArg instead of some
aArg
Am I right that the first version should use local, but not the second?
You don't need to declare the local in the first example.
In fact some languages (like Python I think) prohibit redeclaring an existing identifier (in local scopes).
It looks confusing and may lead to bugs.
What about the case where the default value can also be false
I think this should still work:
Code: Select all
function my_func(someArg)
someArg = someArg or false
end
Re: Lua local question
Posted: Tue Aug 16, 2011 3:25 pm
by kraftman
Code: Select all
function my_func(someArg)
someArg = someArg or false
end
This is redundant: it doesn't change anything.
Re: Lua local question
Posted: Tue Aug 16, 2011 3:28 pm
by ivan
kraftman wrote:This is redundant: it doesn't change anything.
If someArg was nil, it would change its value to a boolean false.
Re: Lua local question
Posted: Tue Aug 16, 2011 4:09 pm
by Taehl
All arguments passed to a function are automatically local. I think the manual says that, but it's good to know.
Re: Lua local question
Posted: Tue Aug 16, 2011 4:23 pm
by benloran
ivan wrote:What about the case where the default value can also be false
I think this should still work:
Code: Select all
function my_func(someArg)
someArg = someArg or false
end
However, an easy bug to run into is the case where someArg is false (assuming someDefaultValue is not false):
Code: Select all
local someDefaultValue = 20
function my_func(someArg)
someArg = someArg or someDefaultValue
print(someArg)
end
my_func() -- 20
my_func('something') -- something
my_func(false) -- 20
If you actually want someArg to be able to contain a boolean value, then you want to explicitly check for nil. It just depends on whether you care about boolean values or not inside my_func.
Re: Lua local question
Posted: Tue Aug 16, 2011 6:10 pm
by pancakepalace
Thanks for all your quick responses guys.
1) I'm a little surprised that local is not needed in my first example since all the code Iv'e looked at for Love always seems to use it. I'm thinking of Hump as an example.
2) I should have been more specific. My parameter can be boolean value or a string and the default is not false but a string so I must implicitly check for nil.
Re: Lua local question
Posted: Tue Aug 16, 2011 6:53 pm
by slime
pancakepalace wrote:Hi guys,
I read all the docs I could find on the Lua language, but the use of local keyword when creating default values for function parameters is still unclear to my mind.
I see this a lot. I imagine the local keyword should be used in this instance.
Code: Select all
function my_func(someArg)
local someArg = someaArg or 'someDefaultValue'
end
What about the case where the default value can also be false. This is how I code it at the moment, but I'm not sure if it's the best. I'm not using the local keyword as I am assuming it is not needed with function parameters.
Code: Select all
function my_func(someArg)
if someArg == nil then someArg = someDefaultValue end
end
Am I right that the first version should use local, but not the second?
#1: someArg is already local to my_func because it's a parameter to the function, so you don't need to redeclare it as local.
#2: That will work, and so will this:
Code: Select all
function my_func(someArg)
someArg = someArg == nil and someDefaultValue or someArg
end
I find my way a little cleaner, but there isn't much of a performance difference either way I think, so it's up to you.
Re: Lua local question
Posted: Tue Aug 16, 2011 7:23 pm
by benloran
slime wrote:Code: Select all
function my_func(someArg)
someArg = someArg == nil and someDefaultValue or someArg
end
Unless someDefaultValue is false, in which case the OP's solution is the only way to do it.