Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:83244 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 37597 invoked from network); 19 Feb 2015 22:19:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Feb 2015 22:19:33 -0000 Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.182 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.214.182 mail-ob0-f182.google.com Received: from [209.85.214.182] ([209.85.214.182:36610] helo=mail-ob0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2C/44-10294-37166E45 for ; Thu, 19 Feb 2015 17:19:32 -0500 Received: by mail-ob0-f182.google.com with SMTP id nt9so19667734obb.13 for ; Thu, 19 Feb 2015 14:19:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type:content-transfer-encoding; bh=DEHtsuA/FvRrucll+HQ4kjUpuvFXPs4/y1wTBULn368=; b=ob/UPHcpQtkNK4kAStFXtYVpZ4Gzkpw2+FhLVUx+F9QaEmIfeeX+WXs49P/Ce2llx3 LlBQbltJvdvsYcCGnBQF31xxxKJw1L2ptNbUDyvCmyGoUkohKGbjNkl287K6SAG0WfvQ NbpYAnb838y7vyjoIZUM/fWuN0Sg8G1yB/73tSfjVhj5IZwJ9pDcDgL6NhZyy7Wflrsx elprNHpumX5m8JOA4zl7V1s8KLmUvzX5pZBukuyMcDuuATPVh8ipAwvus6gavs6udJJf V2KPOH9f8fgkdfB6q1FtC43OyiTxHZWCwZuNoddpjcmAxHuTlCPms70I+xllJoolB8F+ U3Bg== MIME-Version: 1.0 X-Received: by 10.60.174.73 with SMTP id bq9mr4494566oec.47.1424384368723; Thu, 19 Feb 2015 14:19:28 -0800 (PST) Sender: morrison.levi@gmail.com Received: by 10.76.90.74 with HTTP; Thu, 19 Feb 2015 14:19:28 -0800 (PST) In-Reply-To: References: Date: Thu, 19 Feb 2015 15:19:28 -0700 X-Google-Sender-Auth: diuz_BbCOA6VndsabG9DLxr_Jr4 Message-ID: To: Adam Harvey Cc: PHP internals Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC] Comparable: the revenge From: levim@php.net (Levi Morrison) > Those of you with long memories will remember that I proposed a > Comparable interface way back in the pre-5.4 days, but withdrew it > when it became obvious that there was no consensus for it as a feature > and that a vote was likely to fail. > > RFC: https://wiki.php.net/rfc/comparable > PR: https://github.com/php/php-src/pull/1097 > > Why reanimate it now, I hear you ask? I think that comparisons have > only become more prominent in the language: we now have a spaceship > operator for explicit comparisons, yet the behaviour of object > comparisons can be obscure, to say the least, and the user has no > control over how their objects are compared. > > At this stage, I intend to put this up for a vote on March 5 (vote > ending March 12), with the obvious endgame being that this would be > included in 7.0. If the responsibility of comparing two objects is pushed into the objects themselves then the ability to use different comparison criteria for the same two objects when placing them in two different structures is taken away. This is actually a very common problem I have seen people complain about in Java. Another issue: it allows comparing an object to non-objects (even though the stated goal is only to compare two objects of the same type): class MyClass implements Comparable { private $val =3D 0; function compareTo($a) { return $this->val <=3D> $a; } } $int =3D 10; $myClass =3D new MyClass(); $myClass <=3D> $int; // works $int <=3D> $myClass; // if this doesn't produce a warning it at least doesn't behave the same as the line above it Ultimately, when comparing two objects it is usually as part of some algorithm, so instead the Comparator interface could be used: interface Comparator { function compare($a, $b): int; } But even here I would rather just take a function instead of requiring it to be the instance of some interface: function sort($input, callable $comparator($a, $b): int) { /* =E2=80=A6 */ } All in all, I don't think the fundamental idea of the RFC is good. The Comparable interface is inferior to many other techniques of achieving the same goal of comparing two objects. I do want to thank you for taking the time to cite arguments, prior history and an alternative in the RFC. You have done a pretty good job on the RFC itself, in my opinion.