user2206636
user2206636

Reputation: 163

How to declare a variable without initting it to nil in Lua?

I've found it very useful to do something like:

if not variable then
    variable = value
end

Of course, I'd usually rather that variable was local, but I can't declare it local in my if, or it won't be accessible.

So sometimes I do:

local variable
if not variable then
    variable = value
end

The problem is that when I iterate over this code, the variable declaration sets the variable equal to nil. If I can live with a global value (I can), I can get around it by just not declaring the variable outside of the if block.

But isn't there some way that I can both have my local value and let it keep its value?

Upvotes: 2

Views: 15475

Answers (2)

Dan Carver
Dan Carver

Reputation: 1

There's a simple way to test the scope of locals inside loops:

local a = "this is external a"
for x = 1, 3 do
    print(a)
    local a = "THIS IS INTERNAL a"
    print(a)
end

If you run this you'll get:

this is external a
THIS IS INTERNAL a
this is external a
THIS IS INTERNAL a
this is external a
THIS IS INTERNAL a

If the local a inside the loop survived to the next iteration then after the first print it should have printed only "THIS IS INTERNAL a" as the internal a would shadow the external a. The two strings alternating means that the internal local a never survives the bottom of the loop.

Of course, even if it did survive you would have the problem of the internal local declaration shadowing the previous iteration's local a each time through the loop. Essentially you would be unable to access the previous iteration's version of a and by the third iteration you would have 3 separate internal locals, only the last of which would be accessible. It would be the same as if you had done this:

local a = "THIS IS INTERNAL a"
local a = "THIS IS INTERNAL a"
local a = "THIS IS INTERNAL a"

Upvotes: 0

Martin Ender
Martin Ender

Reputation: 44259

First off, the way or is defined in Lua gives you a nice idiom to avoid the if altogether:

variable = variable or value

If variable is nil, or will evaluate to its second operand. Of course, this will only work, if false is not a valid value for variable (because false and nil are both "false" as far as or is concerned).

However, you still have the problem that you need to declare the variable somewhere. I suppose your problem is that in the case of a global loop you think you have to do either:

while condition do
    variable = variable or value
    process(variable)
end

(which would make variable global) or

while condition do
    local variable
    variable = variable or value
    process(variable)
end

which is pointless because local limits the scope to one iteration and reinitializes variable as `nil.

What you can do though is create another block that limits the scope of local variables but does nothing else:

do
    local variable
    while condition do
        variable = variable or value
        process(variable)
    end
end

Upvotes: 7

Related Questions