Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121413 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 4084 invoked from network); 18 Oct 2023 17:20:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Oct 2023 17:20:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 33B831804BC for ; Wed, 18 Oct 2023 10:20:51 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.47]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 18 Oct 2023 10:20:50 -0700 (PDT) Received: by mail-lf1-f47.google.com with SMTP id 2adb3069b0e04-507cee17b00so48775e87.2 for ; Wed, 18 Oct 2023 10:20:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697649649; x=1698254449; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=d3mVzB4FYTYF1f+GqET3GzZ4sAFfGqKPPAHzkiTFYs8=; b=LelUzaxpmX5ex+F42jSr+Jo3LW1EA6q7a7mZ9QbYNK7z+wrIxARBjvoWRhSdKXzMB3 dMdnye1syY9l9MvbBZDwwYO4wdYmSnLyF2i6A6FTmHcnXjjfCzRAY3qdMpFCqqYF+40N Qz3jkTI8wBd4Nvddi3VU5C5ecaC1f/2d5ASiHcLU1aq+rAJvwYjJ7sngG3Vwdb8EIflJ /68IUeHzi6UTcTizLyzFw3IWyBeX5VjkTJQw9BXxGWxuoCvkSBTMGwI67rgi5xFf/paT SRpKYdDofqIXrWAnUznMsybzwZZ++DSsqY2epQLU5ZuWvYRex444r/Eh+3iRBQ5dcm0L 2buQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697649649; x=1698254449; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=d3mVzB4FYTYF1f+GqET3GzZ4sAFfGqKPPAHzkiTFYs8=; b=VeNvO53Th3FOICLDOLDcsGoxztc7kKnPNkVfu7qFIwH4ntsckP2/q+8GifEp2y/yRX DGT1Nov+vOyFo8/RZJGSrmQ8cVsTrssD1l+63S+9YWqWtgskxwLwwOlNyZsL4hchTf3A pxDq4n6PIXVArU0y2hbww8ZyZWb3D6VdLGoW9/t1Le9pRwogSE4mP6llPTsXcgO7Dqrf qJf+wUtjvrMNtD06RNk0vnEuFlH+W9PeAhDH/E4tzLSajtw6abB2/i5/ImB8nkDpFg6f YBNC5uqeMmdTLWE3zXv2xJExZY3n+CfVF6Rz8gTWh2RkPZYrlOY8bgCaLntfH4WwCs9e 6FxQ== X-Gm-Message-State: AOJu0YxagWKNunnJ8S6alyd+3Trg5v/gqwvo0QHG/12mSzpXUG1U6p4D IFzvsdu8VWGyhrG9pn8Ub9Thgqowv4w9uPirCfA= X-Google-Smtp-Source: AGHT+IGfFwtmxbtWRIU8iZMIT5N4hyr6NvuKi95CVVBpGqyk8iqtIHlYITFu+ib7qUUeCmIitxMw+4JjP8VpsDPk0v8= X-Received: by 2002:a19:f815:0:b0:507:a62c:5d0c with SMTP id a21-20020a19f815000000b00507a62c5d0cmr4647501lff.19.1697649648870; Wed, 18 Oct 2023 10:20:48 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 18 Oct 2023 19:20:23 +0200 Message-ID: To: someniatko Cc: php internals Content-Type: multipart/alternative; boundary="000000000000462a68060800dcc3" Subject: Re: [PHP-DEV] Custom object equality From: kjarli@gmail.com (Lynn) --000000000000462a68060800dcc3 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Oct 18, 2023 at 2:51=E2=80=AFPM someniatko w= rote: > Hi internals, > > There is often a need to compare whether two objects are equal. For > example, a popular [brick/money]( > https://packagist.org/packages/brick/money) > library has a `Money` class, which has an `equals()` method. However, thi= s > becomes tedious to implement such methods, when multiple nested objects a= re > involved. For instance, Money has an amount and a currency. > > There were already suggestions on the mailing list to allow "overloading" > existing `=3D=3D` operator, and some suggestions went even as far as > overloading `<`, `>=3D` etc operators. However, overloading existing > operators may lead to a BC break between the PHP version which does suppo= rt > the functionality, and which doesn't. It won't result in a BC break in ca= se > it's an entirely new syntax, because a library or a project won't be able > to write code which overloads `=3D=3D` in say PHP 8.4, but with code whic= h > still works in PHP 8.3 - it will have to require PHP 8.4+. But if it is > implemented as a magic method, then `=3D=3D` will work differently for 8.= 3 and > 8.4. > > I suggest thinking about introducing a new operator `~=3D`, which signifi= es > that custom equality is requested. In such a case, `=3D=3D` will work as= it > works now, and by default `~=3D` will work also like `=3D=3D`, unless its > behavior is overwritten via a magic method. If a magic method is not > present in a class being compared, `~=3D` will compare two objects field = by > field, but using `~=3D` comparison rather than `=3D=3D` comparison, recur= sively. > > For instance, a Money object may consist of Amount and Currency. If two > Moneys are compared using `~=3D`, and Money does not implement a magic > method, Amount also doesn't implement it, but Currency does, then Amounts > are compared using `~=3D` which is equal to `=3D=3D` comparison in this = case, > but Currencies are compared using their custom comparison logic. > > This approach allows combining > - no BC break - `~=3D` is a new syntax which is unavailable in older PHP > versions > - explicitly showing an intent that objects are compared using a custom > comparison, rather than standard PHP one > - allow to skip writing boilerplate equals() methods which just forward > equals() to the nested objects > - standardize such comparisons on the language level > > Of course how exactly this operator looks may be changed, `~=3D` is just = an > example. > > WDYT? > > Regards, > Illia / someniatko > Note that `~=3D` is used in Lua instead of `!=3D`. Having this operator be = an overloading =3D=3D would feel really counterintuitive to me. We have "instanceof", and I've seen "is" ideas floating around. Could also look into the direction of "equals" for this. https://www.lua.org/pil/3.2.html --000000000000462a68060800dcc3--