Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121391 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 51392 invoked from network); 18 Oct 2023 13:12:47 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Oct 2023 13:12:47 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BF2B5180504 for ; Wed, 18 Oct 2023 06:12:46 -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, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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-oo1-f48.google.com (mail-oo1-f48.google.com [209.85.161.48]) (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 06:12:46 -0700 (PDT) Received: by mail-oo1-f48.google.com with SMTP id 006d021491bc7-58111e624bdso4051268eaf.3 for ; Wed, 18 Oct 2023 06:12:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697634765; x=1698239565; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Lw7k+jpM7xKhATFF2+Q7ikAKyhgZCe08NP5qemm4YTU=; b=XileCCoUnIRy4QCEltrt3/SAVqZnsvL4m0z3sV4k/Hs62u5diwT1IRu9yo4RThk1Ex p7m7bT0Ab8vyj5EmAgcgz2nd+zaCFhjTGVUiNGEG4hHRn/Mnq87+vWtE532t92LPbOwc yKIhj0rTzd1eJvxxaLXLzdYBBsIluTHfsPNSfvyir1R0AAPK2SfhHQJhhuGlM149mopo DaPiLBsyQ6ZxztLK2rY2CpArbdqjnviFQiqW85QVmK0ktx+UOB22hXwCQzW7uNtEg4s1 wTKQOsA5vatzvMnjlo6aYW3mnecx+zf7Jo+v28zhvX3XVsi1fdc2taLlRoU+PqLzqUs1 k/Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697634765; x=1698239565; h=content-transfer-encoding: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=Lw7k+jpM7xKhATFF2+Q7ikAKyhgZCe08NP5qemm4YTU=; b=WHC2QFwbOaTvHdfg4iVfLlYxgTDMw7AmFnlb5hdlG6QRFxWl/FRdfEvJX6LoaKTCeP FIztyDvCOBVNiSRAnm6MzAnonIexOaBQ64e3yHKvsvLCz35ZSHiUmibS/CkAZiQB6SX5 Y9jLheO4Pj+OCwhg+uvC0OJzeDIKdOouiHiVNGf/RB9IhJiJAoNPcgk/ZpgnlmnG//cP k63pjpWnol5YTpWEtZF916kaaW8OmCbKNMczyyWUwR9XqzZk09htgfJrIiJcDL1P0Ely HBmbrG/A1Tv2snIqX28WjQUHi95VLH3OB1zSTzxKVPdA/l2rBmoGUKFoojoQ+svOPBV3 N2UA== X-Gm-Message-State: AOJu0Yym88Wjtgx5qp7mxePpufu1imdFy05N83hwgjAWDhn9rUkR/XXS Io07FNbTsof2V96/z3jY8fgYywTB4AW2VpNx51M= X-Google-Smtp-Source: AGHT+IGOz2v251X1WDfvPh1hH8eRxFQPyvPmXvKotCx5+Ln0sbNqJJXbQTI9TyF/D30sttve/be8yf6Qu9QSKKlWfmI= X-Received: by 2002:a4a:dcd8:0:b0:571:28d5:2c71 with SMTP id h24-20020a4adcd8000000b0057128d52c71mr4642848oou.2.1697634765633; Wed, 18 Oct 2023 06:12:45 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 18 Oct 2023 15:12:34 +0200 Message-ID: To: someniatko Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Custom object equality From: landers.robert@gmail.com (Robert Landers) 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/mon= ey) > 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 I like it. One thing I really like about this proposal is that, in theory, it could open the door to new operators like ~<, ~<=3D, ~!, ~=3D which can be similarly implemented. It also makes me wonder if something similar to scala can be implemented, where any operator beginning with "~" can just call an associated magic method. I'm not sure how it would be implemented though, since I don't think you can use symbols/operators in method names. But if you wanted a pipe operator, you could add one via ~|. It'd allow for more useful DSL's that are implemented in PHP, as currently, it doesn't really make sense to create a DSL that is actually PHP since you can't override any operators. That being said, my only criticism is that it will be really easy to forget the ~ when writing code. It's a true shame that overloading wasn't added to the language, as it makes it an impractical language for entire classes of problems/solutions. Robert Landers Software Engineer Utrecht NL