toboolean function

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
TurtleP
Party member
Posts: 147
Joined: Thu Mar 22, 2012 9:20 pm
Contact:

toboolean function

Post by TurtleP »

Hey guys,

Not sure if anyone has done this before, but I made one.

Tired of having saved boolean to a text file and then need that boolean for your game once it loads? Well, toboolean does what it says, turns the value into a boolean! :ultraglee:

I hope you guys enjoy this. You don't have to credit me if you do use it, but whatever.
Attachments
tobooleanexample.love
(561 Bytes) Downloaded 134 times
User avatar
qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

Re: toboolean function

Post by qaisjp »

[Edit 2019]: I don't know why I was being a cunt here, sorry all


Sorry for the harshness:

Your code:

Code: Select all

function toboolean(value)
	if type(value) == "string" then
		if value == "true" then
			return true
		else
			return false
		end
	elseif type(value) == "number" then
		if value == 1 then
			return true
		elseif value == 0 then
			return false
		else
			return nil
		end
	end
end
Better code:

Code: Select all

function tobool(v)
    return v and ( (type(v)=="number") and (v==1) or ( (type(v)=="string") and (v=="true") ) )
end
Moral of the story?
Don't be attention seeking and release simple stuff like this into a .love or in the forum. Use the wiki to document this snippet.
Last edited by qaisjp on Sun Sep 08, 2019 11:14 am, edited 2 times in total.
Lua is not an acronym.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: toboolean function

Post by Roland_Yonaba »

I'd like to point out that, to Lua, 0 is not false.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: toboolean function

Post by Robin »

Even better: for strings, use foo == "true", for numbers, use foo != 0. If you don't know what type you're dealing with, there is probably something wrong with your design.
Help us help you: attach a .love.
scutheotaku
Party member
Posts: 235
Joined: Sat Dec 15, 2012 6:54 am

Re: toboolean function

Post by scutheotaku »

Interesting work, though, as the others suggested, this definitely isn't the best way to go about this.
qaisjp wrote: Moral of the story?
Don't be attention seeking and release simple stuff like this into a .love or in the forum. Use the wiki to document this snippet.
Not to go o/t, but this seems a little too harsh... I mean, why assume that he was "attention seeking"? Isn't it far more likely that he either wasn't aware of the snippets part of the wiki or didn't think of putting it there? Besides, this is definitely not the only "snippet" posted in Projects and Demos, so it's conceivable that he saw those and assumed that this was the place to post such things. Please don't take this as flaming or self-righteousness or anything like that - this just seemed overboard to someone who (like me) is a relative newcomer.
User avatar
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

Re: toboolean function

Post by Xgoff »

my `toboolean` function would simply be

Code: Select all

function toboolean(o) return not not o end
lua already has specific semantics for objects being converted to boolean values, so peeking into the actual value to determine truthiness is dodgy

[de]serialization is an exception of course, but i would choose a different name for the function in that case
User avatar
TurtleP
Party member
Posts: 147
Joined: Thu Mar 22, 2012 9:20 pm
Contact:

Re: toboolean function

Post by TurtleP »

qaisjp wrote:Sorry for the harshness:

Your code:

Code: Select all

function toboolean(value)
	if type(value) == "string" then
		if value == "true" then
			return true
		else
			return false
		end
	elseif type(value) == "number" then
		if value == 1 then
			return true
		elseif value == 0 then
			return false
		else
			return nil
		end
	end
end
Better code:

Code: Select all

function tobool(v)
    return v and ( (type(v)=="number") and (v==1) or ( (type(v)=="string") and (v=="true") ) )
end
Moral of the story?
Don't be attention seeking and release simple stuff like this into a .love or in the forum. Use the wiki to document this snippet.
Thanks I guess, but I'd also check for a "false" string type. But like what scutheotaku said, that was harsh. I'm definitely not seeking attention.
Post Reply

Who is online

Users browsing this forum: No registered users and 82 guests