Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102538 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 38027 invoked from network); 30 Jun 2018 18:04:48 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Jun 2018 18:04:48 -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.217.169 as permitted sender) X-PHP-List-Original-Sender: rudolf.theunissen@gmail.com X-Host-Fingerprint: 209.85.217.169 mail-ua0-f169.google.com Received: from [209.85.217.169] ([209.85.217.169:43078] helo=mail-ua0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 90/E1-15351-F36C73B5 for ; Sat, 30 Jun 2018 14:04:47 -0400 Received: by mail-ua0-f169.google.com with SMTP id x24-v6so1026673ual.10 for ; Sat, 30 Jun 2018 11:04:47 -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=79R+9YfwT5iqkF6kjOUcQZJ0nFCiyNz9a1qy26YpDEc=; b=n66d+tz+HGkLOXjTXephxlH9b7FGbKCODNU8DRBpSpjqNFQ0/8VncNLbsIljk3lv44 J82R3NyDpip3G9aETJmayKu3Hqb3GGiN/P/3nXaRbh/ps23cMbZvt1NgfJP89L91ba1j lz+cGZ02LpFvazvh1iycqhBb7SwiFrNenpCLqYBMHlvAicFpAWqalXBz6zC3NWGoCC/i lZCYg1Atke+Muo3gg838KVJ/lSw0U7XI9S5EaV4Ldb43JsBXr/gQ+IfPc8QfKRaokuqM Kk96hQMOhEAQzy7Zgqe/WP/1jFPPhvkl4FUmSGDzWNW7y9lrPFeEMxOs05aXfvCcmdmW l3fw== 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=79R+9YfwT5iqkF6kjOUcQZJ0nFCiyNz9a1qy26YpDEc=; b=PMREJWckVv81ppWnWenBQv5l+DlIUTA2crvvW0Xb8CsKJl7mQfHQtPVe9o9ylg9OuV dxBN7rC3XUwsHD1+miuUv0/pHel+9y+PTyk9GM+VFKLIviHM9QOFUc7B+7bDLNxEqrsx ka/rZwduguWPFmrhoqLQ4ddSpgNWi0u2OEVNVSlfXv4KIoEsYynTZDgQxAIeTVFlwZ8p +iGc6yEaT4rgxf2fOK5XJALH/qcJpwUyICmXylRCKt/Y/kJf0QE/bh0z6yj7NBlE8kLx NY0eeoCrDIBgmtSf6Z3+VXcoN6TxbaaDMZt6H3IQkFF0yGD8ELWqwPCtFRRpmGH2k026 FKsA== X-Gm-Message-State: APt69E2ijS6kCyKBIZ/UP8Mk+8EDB/lxUlEtainhxp/RZ24l3v14qP/u EhCWRXwEDOSiVpgJHPPnnhXm/dl+ck+MOjKiSIw= X-Google-Smtp-Source: AAOMgpfHPFEjUvZsZdp4mi1TtfFFxAz4td9YV1TjrTNPul7ENcynMKCAK1JItrfc9UoxzJmKMUjyxSDXtP4DFDdUyhQ= X-Received: by 2002:a9f:35b1:: with SMTP id t46-v6mr10862336uad.190.1530381885080; Sat, 30 Jun 2018 11:04:45 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 30 Jun 2018 14:04:27 -0400 Message-ID: To: rowan.collins@gmail.com Cc: levim@php.net, internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000a187d6056fdfcc12" Subject: Re: [PHP-DEV] [RFC] User-defined object comparison From: rudolf.theunissen@gmail.com (Rudolf Theunissen) --000000000000a187d6056fdfcc12 Content-Type: text/plain; charset="UTF-8" > I think it's fairly crucial that this proposal is NOT just about overloading operators, it's about overloading *behaviour* That is what this RFC is trying to achieve. Equivalent in behaviour, so we're providing a way for a class to define how it compares or equates to another value. We're not providing a way to override the meaning or function of the comparison operators, and it's up to the implementor to follow this advice. There's nothing stopping someone making `>` modify the object. But that's the nature of user-defined behaviour of operators: giving a developer more flexibility also increases the amount of weird stuff they can do. The question is simply whether the value of the flexibility is more than the cost of the potential for misuse. We're proposing the ability for a user to determine how a class should be compared to another value. The ability to change the semantics of the comparison operators is an unavoidable side effect. > If someone implements __compareTo() just to give some domain-specific meaning to the <=> operator That would be bad implementation on their part, and we should mention in the documentation update that this is not the intended application. > I think __compareTo() and __equals() could act more like a return type hint, and either throw a TypeError straight away, or attempt to coerce to the correct type and throw a TypeError on failure. Currently they do attempt to convert to boolean and integer, respectively, but that doesn't achieve much because you can still return nothing in either case and the coercion would be 0 and false. -- The decision whether to accept this will come down to the trade-off between flexibility and potential for misuse. We're giving the developer more power and responsibility by adding to the capability of the language, but that can be said for any language-level change that has the potential for misuse. I would personally opt for more flexibility, with clear direction on the intended use, and trust that those who decide to leverage it know what they're doing. On Fri, 29 Jun 2018 at 13:35, Rowan Collins wrote: > On 27 June 2018 at 19:24, Levi Morrison wrote: > >> This permits things such as NumPy to overload < to do an element-wise >> comparison of the members; something like this: >> >> np.array([1, 3, 5, 7]) < np.array([2, 1, 6, 6]) >> // evaluates to np.array([true, false, true, false) >> > > > In my opinion, this is exactly the kind of messy operator overloading we > should avoid, but if we DO want it, then its purpose is to over-ride the > syntax, not the meaning, of the operator; so "overloaded <=>" would NOT be > the same as "overloaded comparison". > > I think it's fairly crucial that this proposal is NOT just about > overloading operators, it's about overloading *behaviour* which occurs > throughout the language, and as such I think this statement from the RFC is > incorrect: > > > __compareTo is equivalent to <=> and __equals is equivalent to == > > If someone implements __compareTo() just to give some domain-specific > meaning to the <=> operator, what will happen when someone passes those > objects to sort()? If someone decides they want to return an object from > __equals(), how will switch statements behave? And so on... > > Because of that, I would much rather these functions did enforce a return > type. Note that __toString() generates an error for an incorrect return > type when used, and __sleep() raises a Notice and serializes Null instead > of the object. I think __compareTo() and __equals() could act more like a > return type hint, and either throw a TypeError straight away, or attempt to > coerce to the correct type and throw a TypeError on failure. > > Then, if we want the ability to override the <=> operator specifically, > that can be added later, and users can return whatever type they want > without breaking all the other places where __compareTo() is called. > > Regards, > -- > Rowan Collins > [IMSoP] > --000000000000a187d6056fdfcc12--