View Single Post
11-15-16, 12:27 PM   #21
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Originally Posted by Seerah View Post
But nil isn't the same as false.
Of course, I mentioned that condition in my post. My expression only works as expected if item ID never is false.

Originally Posted by SDPhantom View Post
Again, differentiating false in this situation is overkill. The API never returns false in place of a value that isn't normally boolean. As a matter of fact, API that returns data often doesn't provide any returns when there isn't any available, not even nil. However, when Lua attempts to assign no value to a variable, it ends up assigning nil anyway.
I already said I was talking from a readability perspective. I think mine reads better, that's it. However I went ahead and timed it and mine and Phanx expressions are actually faster when the value is truthy. Mine is a bit slower when the value is nil.

Originally Posted by _Max_Cavalera_ View Post
Lua Code:
  1. MainFrame:SetShown(itemid and true or false);

Can you explain this line to me? I think that it's supposed to be, show frame if itemID exists?! but I don't get how the and true or false works.
In this case it works like so; the expression "itemid and true or false" evaluates to true if itemid is truthy (any value except nil and false), otherwise evaluates to false. It's essentially a way to write the following in a single expression:
Code:
if itemid then
	return true
else
	return false
end
It doesn't have to return true or false, either. Your example with the two different strings works exactly the same.

You can read more about it here:
http://www.lua.org/pil/3.3.html
__________________
Grab your sword and fight the Horde!

Last edited by Lombra : 11-15-16 at 12:33 PM.
  Reply With Quote