Passing False into a function

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Passing False into a function

Post 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.
Kurosuke needs beta testers
User avatar
slime
Solid Snayke
Posts: 3133
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Passing False into a function

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Passing False into a function

Post 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 
When I write def I mean function.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Passing False into a function

Post 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.
Shallow indentations.
User avatar
slime
Solid Snayke
Posts: 3133
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Passing False into a function

Post by slime »

You could simplify that to

Code: Select all

self.visible = visible ~= false
Unless I'm missing something. :p
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Passing False into a function

Post 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.
Kurosuke needs beta testers
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Passing False into a function

Post 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:
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Passing False into a function

Post 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.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 56 guests