Boolean syntax for conditional?

I cannot make this work:

1
2
3
4
5
local flag = true
 
if flag then
   <something>
end

Works fine for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
-- bool test
local flag = true;
 
if flag then
        print("true");
else
        print("false");
end
 
if not flag then
        print("true");
else
        print("false");
end
 
if (flag == true) then
        print("true");
else
        print("false");
end
 
if (not flag == true) then
        print("true");
else
        print("false");
end
 
--[[ outputs
true
false
true
false
--]]

lua has a not equal operator, so you can do if flag ~= true then

I tend to use if var then and if not var then

The thing to remember here though is lua is not a typed language if foo then will be true if foo is true or non nil, similarly if not foo then will be true if foo is false or nil.

Hmmm, I don't know why I wasn't meeting with success before...but it's working fine now. I dunno. (Thank you!)

@MatthewJHanlon: Understood. If var is set to nil, what you describe is what I would expect.

views:1388 update:2011/10/5 8:48:05
corona forums © 2003-2011