Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60367 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 23133 invoked from network); 29 Apr 2012 21:22:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Apr 2012 21:22:33 -0000 Authentication-Results: pb1.pair.com header.from=jille@quis.cx; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=jille@quis.cx; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain quis.cx from 85.223.64.10 cause and error) X-PHP-List-Original-Sender: jille@quis.cx X-Host-Fingerprint: 85.223.64.10 10-64-223.ftth.xms.internl.net Received: from [85.223.64.10] ([85.223.64.10:61506] helo=istud.quis.cx) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CB/A4-13197-811BD9F4 for ; Sun, 29 Apr 2012 17:22:33 -0400 Received: from boom-ubuntu.quis.cx (vhe-430065.sshn.net [195.169.212.153]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by istud.quis.cx (Postfix) with ESMTPSA id 0170090BF for ; Sun, 29 Apr 2012 23:22:28 +0200 (CEST) Message-ID: <4F9DB114.9030408@quis.cx> Date: Sun, 29 Apr 2012 23:22:28 +0200 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.28) Gecko/20120313 Thunderbird/3.1.20 MIME-Version: 1.0 To: internals@lists.php.net References: <4F9AFE3D.8010805@quis.cx> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Function boolval() From: jille@quis.cx (Jille Timmermans) On 28-04-12 06:27, Kalle Sommer Nielsen wrote: > 2012/4/27 Jille Timmermans: >> I suggest we add a function boolval(). It simply converts the given argument >> to a boolean, like strval(), intval() and floatval(). I already have an >> implementation ready[1]. >> >> Why? >> * It is missing in the current list of *val()-functions and people expect it >> to exist. I'd say it is an inconsistency. >> * It can be used as a callback, which is why a bool-cast does not suffice. > > Does it really matter nowadays when we got closures anyway: > > $bools = array_map(range('a', 'z'), function($a){ return((boolean) $a); }); > Closures can achieve the same goal but are less readable as Sebastian Krebs already pointed out and still confuses the programmer. Sherif Ramadan wrote: > Why is this going to be more beneficial to implement? Is it that you > feel readability of the closure is too difficult or that you feel > there is a use case that presents further benefits for implementing a > boolval function, beyond that of just readability or other subjective > preferences? > > Personally, I would feel something like array_filter(range('a','z')); > is way more readable than all of the above and makes a lot more sense > to me. That's just an example of course. (And doesn't even do the same thing) > > Under most circumstances, one does not tend to care about the truth in > expression unless it is truth. So the following code is far more > practical than the suggestions being made here: > > if (!array_filter(array(0,false,null,'', array()))) { > /* The array is made up entirely of falsey values */ > } else { > /* The array is not made up entirely of falsey values */ > } > > I'm not saying the function is useless. I'm just saying PHP already > offers more than 1,000 functions through core, alone, and adding just > one more function that doesn't seem to offer anything new really isn't > sounding like a promising idea to me. We only add to people's > confusion more by offering them dozens of choices to accomplish the > same thing. We've all heard the arguments of "print vs. echo", or X vs > Y. There's no practical reason to include this in core being presented > hear apart from "I like it more..." I do agree it isn't perfect to have both casts and conversion functions but we're too late now anyway. They both exist - but the boolval conversion function is missing, which is - I think - even worse than "yet another function". -- Jille