Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102549 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 14098 invoked from network); 1 Jul 2018 18:45:18 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Jul 2018 18:45:18 -0000 Authentication-Results: pb1.pair.com header.from=rudolf.theunissen@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rudolf.theunissen@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.51 as permitted sender) X-PHP-List-Original-Sender: rudolf.theunissen@gmail.com X-Host-Fingerprint: 209.85.213.51 mail-vk0-f51.google.com Received: from [209.85.213.51] ([209.85.213.51:44238] helo=mail-vk0-f51.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 40/28-15351-D31293B5 for ; Sun, 01 Jul 2018 14:45:17 -0400 Received: by mail-vk0-f51.google.com with SMTP id 125-v6so1375562vke.11 for ; Sun, 01 Jul 2018 11:45:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=IPcq0GQHzp45OCqHR94TVPEzQZoWafhr7kyQ0jTUn04=; b=O12LEKfY2rn6nD1/pqNMloV4RYlgAHnDpnfZTaK2l+fQhAjPTJuIXL2tq7GfYIymp6 mDXpqFCQZP3NBsn2rXvNgcmyn3Coan46OYs7504u8Bt4qQwzYiuhkX2FLctz5mmsrRud YjKeDM7uD4bT2NegFVU55fTzjpcgQharwD89aNWbcV+buyrQOoQrQXxX12MSs4sbZ6q1 mQut6/pFM0LkdFBBHXLdL+3aarHC83Jdp0cWLLHsM9u1EWGyPTZFdwEkICthBZq6l3uX ycf7yOoyTROC8JKUToRmx7u5P8j67ADl4U7e9ihn2cvjKtiXcL1lT5D2z2upUuyJ/ZBx M/gQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=IPcq0GQHzp45OCqHR94TVPEzQZoWafhr7kyQ0jTUn04=; b=WijKINPYkK+Oc9Ma5vJ9gW/rGy0NId15DxB1ClDrN0VZ7hjjLRTzyiJGq/zq6hBwSD lq+1kREPB4d3is+Shh3dJeteu3Sm3Ihh0JWgebVBesK6QevJA8gsmcQdwIOEWN3jQZ7H anZb8pJUAdoTYlcgeUVyPqA89q03gws1qpSVgwgeg21sdSvhkthc8qwZGtsay4Uww9e6 +rbt2CjCFTbXEyyuLEgjrfdxzKa8tvbJB4Y8DMIyWgBjvI/2/GsghgKwx09a2dyhCRG4 VUUGtPUXPdTlacVYZu17aanp3P4gxpRZ+cjnYIp0M78J7Tws99kQV7f3/fETMT9+Ruf1 5ocQ== X-Gm-Message-State: APt69E37aJ2J+0NlpvWfAYbpeyKYCfkrtMyTVEFyEY6qYgbTEkG4Otxg TR12WYj6IUhnDMXtfmIHlUoL/+dGCEE5G2VLMZY= X-Google-Smtp-Source: AAOMgpdHC58UpGMukfIRXPmzAY7PBlJnFSE+gLzwECCJP5geLHYbDNfo80x/yDQCXxb3vLnDeD5Cu3QQ9tq9gw5m2GQ= X-Received: by 2002:a1f:3cca:: with SMTP id j193-v6mr13109946vka.156.1530470714616; Sun, 01 Jul 2018 11:45:14 -0700 (PDT) MIME-Version: 1.0 References: <91F0875C-74EC-4115-83F9-5E87A02051C9@gmail.com> In-Reply-To: <91F0875C-74EC-4115-83F9-5E87A02051C9@gmail.com> Date: Sun, 1 Jul 2018 14:45:03 -0400 Message-ID: To: rowan.collins@gmail.com Cc: levim@php.net, internals@lists.php.net Content-Type: multipart/alternative; boundary="00000000000048b20e056ff47b04" Subject: Re: [PHP-DEV] [RFC] User-defined object comparison From: rudolf.theunissen@gmail.com (Rudolf Theunissen) --00000000000048b20e056ff47b04 Content-Type: text/plain; charset="UTF-8" > > I think what I was suggesting is that if people in future want to overload > the operators specifically, we would want to provide an overload for <=> > that was separate from __compareTo, so that you could overload that without > breaking things like sorting. I think it would be confusing to have both, or to have <=> not be aligned with __compareTo, because they are semantically equivalent. By overloading <=>, you have to also override comparison. Separating those is a recipe for confusion. Apologies if this is covered in the RFC, but does that apply wherever they > are used? For instance, what will the following code do? > class SpaceStation { > public $shape; > public function __construct($shape='[]') { > $this->shape = $shape; > } > public function __compareTo($other) { > return new self($this->shape . '>=<' . $other->shape); > } > } > $a = new SpaceStation; > $b = new SpaceStation; > $c = $a <=> $b; // Error? 0? > echo $c->shape; // or will this echo '[]>=<[]'? > $d = $a < $b; // Same result? > $list = [$a, $b, $c, $d]; > sort($list); // Or will the error only be raised here? When you use the `<=>` operator, the return value is normalised to either -1, 0 or 1 which is consistent with the current behaviour. So the object you're returning in `compareTo` will be converted to an integer(1) which will make `$c` be `1`. Attempting to call `echo $c->shape` then makes no sense. `$a < $b` will be false because according to your return of `1`, `$a` would be greater. Sorting would then consider the LHS to always be greater also. One thing that your example brings up is that `<=>` is not exactly equivalent to calling `__compareTo` directly, which would just return the raw value. This is exciting because it's a new issue that I'll add to the RFC for consideration. Ideally we would like the operator and the magic method to behave exactly the same, but that doesn't seem like a viable option because we wouldn't want to magically translate the return value of a function you're calling directly. The only option then is to simply advise that <=> will normalise the return value of `__compareTo`. I'm personally okay with that behaviour if it means the convention of the return value of <=> remains intact. --00000000000048b20e056ff47b04--