Lua local question

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
pancakepalace
Prole
Posts: 40
Joined: Wed Aug 03, 2011 3:13 pm

Lua local question

Post 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?
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: Lua local question

Post 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.
User avatar
ivan
Party member
Posts: 1912
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Lua local question

Post by ivan »

Code: Select all

function my_func(someArg)
     local someArg = someaArg or 'someDefaultValue'
end
I think you mean someArg instead of someaArg :)
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
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: Lua local question

Post by kraftman »

Code: Select all

function my_func(someArg)
     someArg = someArg or false
end
This is redundant: it doesn't change anything.
User avatar
ivan
Party member
Posts: 1912
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Lua local question

Post 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.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Lua local question

Post by Taehl »

All arguments passed to a function are automatically local. I think the manual says that, but it's good to know.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
benloran
Prole
Posts: 19
Joined: Tue Jul 05, 2011 4:52 pm

Re: Lua local question

Post 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.
pancakepalace
Prole
Posts: 40
Joined: Wed Aug 03, 2011 3:13 pm

Re: Lua local question

Post 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.
User avatar
slime
Solid Snayke
Posts: 3144
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Lua local question

Post 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.
User avatar
benloran
Prole
Posts: 19
Joined: Tue Jul 05, 2011 4:52 pm

Re: Lua local question

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests