Wikia

WoWWiki

Changes: API TYPE Boolean

Edit

Back to page

m (See Also)
m
 
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
A '''boolean''' data type is one that by definition only has two states, "true" and "false".
+
{{wowapitype}}
  +
The special WoW 'boolean' API type, use in WoW Lua and the WoW API.
   
== Booleans in Lua ==
+
Typically a '''boolean''' data type is one that by definition only has two states, "true" and "false". The old WoW boolean Type is defined by number 1 and {{api|nil|t=t}}, for true and false. Many older APIs in WoW use these semantics for bool.
  +
  +
== History of Booleans in WoW ==
   
 
In [[Lua]], the programming language used by the World of Warcraft [[User Interface]], real booleans were not introduced until version 5.0.
 
In [[Lua]], the programming language used by the World of Warcraft [[User Interface]], real booleans were not introduced until version 5.0.
   
Early versions of World of Warcraft used Lua v4, which shows in that the majority of [[API]]s still return '''1''' for "true", and '''nil''' for "false".
+
Early versions of World of Warcraft used Lua v4, which shows in that the majority of [[API]]s still return '''1''' for "true", and '''nil''' for "false".
 
   
 
In Lua, there are only two values that evaluate to a boolean false: '''false''' and '''nil'''.
 
In Lua, there are only two values that evaluate to a boolean false: '''false''' and '''nil'''.
Line 11: Line 11:
 
'''ALL OTHER VALUES EVALUATE TO TRUE'''. Including 0, which is an important distinction for e.g. {{wplink|C programming language|C/C++}} programmers.
 
'''ALL OTHER VALUES EVALUATE TO TRUE'''. Including 0, which is an important distinction for e.g. {{wplink|C programming language|C/C++}} programmers.
   
== Gotchas with Booleans in Lua ==
+
== Gotchas with Boolean types in Lua ==
 
 
When used in '''if ... then''' clauses a '''1''' result is the same as a '''true''' result, so you do not have to worry there.
 
When used in '''if ... then''' clauses a '''1''' result is the same as a '''true''' result, so you do not have to worry there.
But beware of ''comparing'' the returns of supposedly-boolean functions with eachother without forcing them to actually be boolean.
+
But beware of ''comparing'' the returns of supposedly-boolean functions with eachother without forcing them to actually be boolean.
   
+
Example:
Example:
 
 
function Func1() return 1; end -- old-style function returning 1 for true
 
function Func1() return 1; end -- old-style function returning 1 for true
 
function Func2() return true; end -- new-style function actually using true
 
function Func2() return true; end -- new-style function actually using true
Line 26: Line 26:
   
 
This would output '''"They're different!"''', because ''1 and true are not the same thing in Lua''.
 
This would output '''"They're different!"''', because ''1 and true are not the same thing in Lua''.
 
   
 
You can force a value to be boolean by NOTing it twice, like so:
 
You can force a value to be boolean by NOTing it twice, like so:

Latest revision as of 04:24, October 30, 2013

API types < Boolean

The special WoW 'boolean' API type, use in WoW Lua and the WoW API.

Typically a boolean data type is one that by definition only has two states, "true" and "false". The old WoW boolean Type is defined by number 1 and nil, for true and false. Many older APIs in WoW use these semantics for bool.

History of Booleans in WoW Edit

In Lua, the programming language used by the World of Warcraft User Interface, real booleans were not introduced until version 5.0.

Early versions of World of Warcraft used Lua v4, which shows in that the majority of APIs still return 1 for "true", and nil for "false".

In Lua, there are only two values that evaluate to a boolean false: false and nil.

ALL OTHER VALUES EVALUATE TO TRUE. Including 0, which is an important distinction for e.g. C/C++ programmers.

Gotchas with Boolean types in Lua Edit

When used in if ... then clauses a 1 result is the same as a true result, so you do not have to worry there. But beware of comparing the returns of supposedly-boolean functions with eachother without forcing them to actually be boolean.

Example:

function Func1() return 1; end     -- old-style function returning 1 for true
function Func2() return true; end  -- new-style function actually using true

if(Func1() == Func2()) then
  print "They're both the same!";
else
  print "They're different!";
end

This would output "They're different!", because 1 and true are not the same thing in Lua.

You can force a value to be boolean by NOTing it twice, like so:

definitelyBoolean = not not kindaBoolean;

In the earlier disfunctional example, we'd get away with NOTing it just once since we're only testing if they're the same:

if( (not Func1()) == (not Func2()) ) then
  ...

See Also Edit

Around Wikia's network

Random Wiki