Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81002 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 83185 invoked from network); 22 Jan 2015 23:47:17 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Jan 2015 23:47:17 -0000 Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 66.111.4.27 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 66.111.4.27 out3-smtp.messagingengine.com Received: from [66.111.4.27] ([66.111.4.27:35818] helo=out3-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5E/44-61273-40C81C45 for ; Thu, 22 Jan 2015 18:47:16 -0500 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id EA0722121C for ; Thu, 22 Jan 2015 18:47:13 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute2.internal (MEProxy); Thu, 22 Jan 2015 18:47:13 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=x-sasl-enc:message-id:date:from :mime-version:to:subject:references:in-reply-to:content-type :content-transfer-encoding; s=smtpout; bh=y+4yZB9m01IvAErTvIVqMF 1R4OE=; b=cqLd8B4sT9XSGaj2fWl8vRm8ByG8A+ZP5k6MjxvpaqOj6jSJwFAWqb x0u0Rk/Ie38nvxaegh0XiM9qy3VpcBRNoU/EIFw3E3VYEMs0mGQO9nJbYhaWusTH KDhSdwFsHT8TM7kygJe4fPT+tRJ91cVvULOMoPtcW3uRrroW9fiiI= X-Sasl-enc: 02lOcP3WHUvdLPPuTAsiWRM7AaJo/OAPeG05O0xvtzb4 1421970433 Received: from [192.168.42.117] (unknown [98.226.241.18]) by mail.messagingengine.com (Postfix) with ESMTPA id A7B8668020E for ; Thu, 22 Jan 2015 18:47:13 -0500 (EST) Message-ID: <54C18C01.9090907@garfieldtech.com> Date: Thu, 22 Jan 2015 17:47:13 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: internals@lists.php.net References: <53F8A1FF-C809-4DDF-9C6F-6916C3E4F044@ajf.me> <3251D635-D97C-4738-B537-935A6CC21E19@ajf.me> <54C17E2B.80708@garfieldtech.com> <73BC4206-BEA9-4416-BBB6-F6F99D9A8623@ajf.me> In-Reply-To: <73BC4206-BEA9-4416-BBB6-F6F99D9A8623@ajf.me> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] [RFC] Combined Comparison (Spaceship) Operator From: larry@garfieldtech.com (Larry Garfield) On 01/22/2015 05:39 PM, Andrea Faulds wrote: > Hi Larry, > >> On 22 Jan 2015, at 22:48, Larry Garfield wrote: >> >> This looks potentially quite useful, especially for multi-stage comparisons as you note. Mainly it would mean I don't have to remember which direction is positive or negative, as I can never get that right without looking it up. :-) > Glad you like it. I, too, sometimes have trouble remembering what direction it goes in. > >> The examples say nothing about mixing types, though. Eg, what would these return: >> >> return 0 <=> "0" >> >> return "" <=> 0 >> >> return 1 <=> [1, 2, 3] >> >> Ignoring object property names and going by the order of the property definition seems like asking for trouble, too. >> >> There may be easy logical answers to the above given PHP's existing type juggling, but they should be called out explicitly in both the RFC and in any subsequent documentation. > The behaviour would be the same as the current <, <=, ==, >= and > operators. If > currently returns TRUE, then it is 1, if == currently returns TRUE, then it is 0, and if < currently returns TRUE, then it is -1. Despite all the ridiculousness of our existing comparison rules, apparently <, == and > returning TRUE are mutually exclusive, thank goodness. > > I don’t really want to cover this behaviour in the RFC, because explaining it in full is a considerable effort and the rules it uses are not new, they are the ones PHP already uses. The behaviour for arrays is perhaps a little odd, but that is our existing behaviour, and I’d rather not introduce inconsistency by making <=> use different rules to the other operators. > > I agree, however, that documentation for this feature should mention the behaviour, possibly just linking to our existing documentation on type juggling. > > Thanks. > > -- > Andrea Faulds > http://ajf.me/ Assuming it's accurate and I'm understanding you correctly, I think the following would be a sufficient statement for the RFC: ----- The behavior of this operator with mixed types is such that the following two snippets produce the exact same result in all cases: return $a <=> $b; if ($a > $b) { return -1; } elseif ($a < $b) { return 1; } else { return 0; } ----- Or maybe that's implicit in the existing link, I'm not sure. (Now that I read it over again.) Basically, in order to understand the more esoteric type juggling that may occur you'd need to mentally expand it to its "uncompressed form", for which the rules are already defined (if possibly quirky in some cases). --Larry Garfield