Page 1 of 1

Passing False into a function

Posted: Wed Jan 26, 2011 5:57 pm
by tentus
Hey, I'm trying to do something like this:

Code: Select all

function object:init(x, y, visible)
	self.x = x or 64
	self.y = y or 64
	self.visible = visible or true
end

function object:draw()
	if self.visible then
		-- drawing code goes here
	end
end
This all works in my head, but it looks like saying object:new(32, 32, false) will make Lua use the "or true", meaning that I can't have a default visibility set.

Am I just showing my newness to Lua or is there some other bug I'm not seeing? I'm a bit under the weather right now (home from work, even) so I won't be offended at all if someone points out a tremendously stupid mistake or obvious error.

Re: Passing False into a function

Posted: Wed Jan 26, 2011 6:02 pm
by slime

Code: Select all

self.visible = visible ~= nil and visible or true
Unless there's a cleaner way to do that, I think that's what you're looking for.

Re: Passing False into a function

Posted: Wed Jan 26, 2011 6:08 pm
by kikito
I prefer the positive version:

Code: Select all

 self.visible = visible == nil and true or visible 
But in general, if you are dealing with falses, trues and nils, it's probably simpler just to use a plain old if. It ain't much longer:

Code: Select all

 if visible == nil then self.visible = true else self.visible = visible end 

Re: Passing False into a function

Posted: Wed Jan 26, 2011 6:11 pm
by Boolsheet
How about:

Code: Select all

self.visible = visible ~= false and true
From the Lua manual ( http://www.lua.org/manual/5.1/manual.html#2.5.3 ):
The disjunction operator or returns its first argument if this value is different from nil and false; otherwise, or returns its second argument.

Re: Passing False into a function

Posted: Wed Jan 26, 2011 6:14 pm
by slime
You could simplify that to

Code: Select all

self.visible = visible ~= false
Unless I'm missing something. :p

Re: Passing False into a function

Posted: Wed Jan 26, 2011 6:18 pm
by tentus
slime wrote:You could simplify that to

Code: Select all

self.visible = visible ~= false
Unless I'm missing something. :p
Nope, that works exactly how I want it to. Now I just have to figure out how to say that in English, so that I can remember it and/or explain it to other people down the line.

Re: Passing False into a function

Posted: Wed Jan 26, 2011 8:35 pm
by vrld
Maybe too late, but if you want your code to look hackish, you could do this:

Code: Select all

self.visible = not not visible
It works like this: If visible is ...
  • true, not visible = false and not not visible = not false = true
  • false, not visible = true and not not visible = not true = false
  • nil, not visible = true and not not visible = not true = false
It's probably more readable to use one of the other solutions though :roll:

Re: Passing False into a function

Posted: Wed Jan 26, 2011 8:46 pm
by Robin
vrld wrote:Maybe too late, but if you want your code to look hackish, you could do this:
I thought about that, but it doesn't work: the default should be true, not false. Therefore, passing nil should be the same as passing true, instead of passing false.

I like slime's solution best. Simple, elegant, non-obfuscating.