Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125623 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id B68CC1A00BD for ; Wed, 18 Sep 2024 15:47:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1726674558; bh=c7aSaTHe/UTaMzop0DGeZjGyHXdsKCTAmzt8OK/FoK8=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=DJ6PQg/N6JHZfahN6llrMb07MmMh/QSmcjx9Os4cWUDf/fSRxXQRrgLB2dUMD2A5j 93p73d6c4WOTv/6krfCTlaehYQC0IhynzzXq273h80CoTnPGrWfAMUeup7r2DRNSWR cq3Eax+YwyCHNph0AAW0wkmu4ZTPmgKSIljkHP3FQ44aO1yYrqakfpD0rFrI9TX9C0 6IZobrssI2jGn308ZVfH2rxC1220nepuxuXRzNUZSNXmUdpE/liShbdGhJf1ZApFEs FtAyVdp4oeun6IQh3mh59ivOoCtkiKDBNPV8fATz3xcYcaSw82s9D9v352evOfZ2tQ KlvgbBL+BfOlQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 540071801DE for ; Wed, 18 Sep 2024 15:49:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f171.google.com (mail-yb1-f171.google.com [209.85.219.171]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 18 Sep 2024 15:49:16 +0000 (UTC) Received: by mail-yb1-f171.google.com with SMTP id 3f1490d57ef6-e201f6eda9fso1112091276.2 for ; Wed, 18 Sep 2024 08:47:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726674430; x=1727279230; 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=dmSwIoxcumQK5E2aDmSeJ0i34VVYDIbnGSjRNHz8k/8=; b=H2jcBS5Q7KiocEjgk2Vpo0u7nu/l2Z+PxVp11Kbeo/rGhYxUqD38qkQQBU/tLi1LCw ABIsePHOaJdFuVtbdYnRkdljQ1bA6upkyube5BzxH0MBdKT5aSp/KPUAf1gLb6ba9hS5 wFNM3jVDSapwOaaUr0nzph+MWXxif5gmkpRGJ9WgHSBz4cuDB1eDNtqk/5u6Ta5sMxd+ /T3wNcQDUI1cbgUU4WOBjg5V/nChKNml9txrmUrbbvJJA+CqVKhcEnfiXs/OXOwrBWE9 Wale1w9zamKdDx2obDMOYfNxJRwoj4oYJKUxpFrfBMNfkj246Vtua1ZqkXnKPrFIiPOa 8dVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726674430; x=1727279230; 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=dmSwIoxcumQK5E2aDmSeJ0i34VVYDIbnGSjRNHz8k/8=; b=DRfobjAxuiJ+Do7gf1bthBtoCvtl/58Stm9SWpRd8YnrzVMYko3Qw2hZKFCLP4ZIcA /6rm00XyP3jEqGUOZVyojvXBxP99TCX1SRgGfhfwKAzMmzijj/59i0jfa8XrDQ9pDx+C vSGEJOrZjYDVZgLWDNmNUafv7RwDEGh4jJ95lyWgTg95f4UXtQhSJfkwEnMiLfbtkhlZ K/JH6ZhKzJAqfnX6/e/+OKj4A5atUc2onoZlph0KstT3/lPyJypIh9j4Wzvv4Tjxj1VP xS6c8YWXfyRtghv1BbgCdHyZatFAxaVdFS2VnSPgzZ2b4zNj9EY0umyQ78ulLuGP6cJG 2y/w== X-Gm-Message-State: AOJu0YzswtBTOLEiZm4nvptE87wH918k/5DxyNbXk3FXHqWjbc7p1Hzx PU5Us8+zC5JWDvgkdgH0x/gYVNiXN8RMBMZLOx/4q8MQOwclLetEILUAOycVWpHxvozNHqem9u3 I5g6oYOI60jzJoGM0+CMPZI2uUwQ= X-Google-Smtp-Source: AGHT+IGT0B71t6XTL5u+zsehIljCv+AVRgr+WNEVsJ1N6i/TdHRI0z51Xhp+FhdzjK7VcTTh4cTIHiaABHgEkYZdUZQ= X-Received: by 2002:a05:6902:2407:b0:e1d:9b03:8812 with SMTP id 3f1490d57ef6-e1db010485emr10919479276.57.1726674430282; Wed, 18 Sep 2024 08:47:10 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <2551c06a-ec1f-4870-a590-aeb5752fc944@rwec.co.uk> <0f0444eb-8fc5-4c56-8528-5aa528988e73@rwec.co.uk> <12de3d03-3426-44f8-a83f-d686d2034912@rwec.co.uk> <3f6d4409-d370-4391-a8e5-a8a50d8da793@gmx.de> In-Reply-To: Date: Wed, 18 Sep 2024 18:46:51 +0300 Message-ID: Subject: Re: [PHP-DEV] [Pre-RFC Discussion] User Defined Operator Overloads (again) To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000000f55d1062266b884" From: drealecs@gmail.com (=?UTF-8?Q?Alexandru_P=C4=83tr=C4=83nescu?=) --0000000000000f55d1062266b884 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Sep 18, 2024 at 6:11=E2=80=AFPM Rowan Tommins [IMSoP] wrote: > On Wed, 18 Sep 2024, at 15:19, Christoph M. Becker wrote: > >> Maybe it would be "useful enough" to just restrict to left-hand side: > > > > In my opinion, this is the only reasonable way to implement operator > > overloads in PHP. It is easy to understand, and what can easily be > > understood is easy to explain, document, and to reason about. I do not > > understand why we're talking about commutative operations; even the > > inconspicuous plus operator is not commutative in PHP > > (https://3v4l.org/nQcL5). > > There are really three different things we shouldn't confuse: > > 1) Commutativity of the operation, as in $a + $b and $b + $a having the > same result. As you say, this is a non-goal; we already have examples of > non-commutative operators in PHP, and there are plenty more that have bee= n > given. > > 2) Commutativity of the *resolution*. This is slightly subtler: if $a and > $b both have implementations of the operator, should $a + $b and $b + $a > call the same implementation? We can say "no", but it may be surprising t= o > some users that if $b is a sub-class of $a, its version of + isn't used b= y > preference. > > 3) Resolution when *only one side has an implementation*. For instance, > how do you define an overload for 1 / $object? Or for (new DateTime) + (n= ew > MySpecialDateOffset)? It's possible to work around this if the custom cla= ss > has to be on the left, but probably not very intuitive. > > It's also worth considering that the *resolution* of PHP's operators > aren't currently determined by their left-hand side, e.g. int + float and > float + int both return a float, which certainly feels like "preferring t= he > float implementation regardless of order", even if PHP doesn't technicall= y > implement it that way. > > > How about doing it like in Python, where there is `__add__` and `__radd__`? And the engine could call $op1->add($op2) if $op1 is an object and `add()` is implemented, or otherwise call $op2->rightAdd($op1) if $op2 is an object and `rightAdd()` is implemented, or otherwise fail with an error. We could have (distinct) interfaces for both `add()` and `rightAdd()`. Or use magic methods like `__add()` and `__rightAdd()` to allow stricter types instead of `mixed` on the other operand. I think there is no extra complexity for the engine by using magic methods or interfaces. --=20 Alex --0000000000000f55d1062266b884 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Wed, Sep 18, 2024 at 6:11=E2=80=AFPM R= owan Tommins [IMSoP] <imsop.php@= rwec.co.uk> wrote:
On Wed, 18 Sep 2024, at 15:19, Christoph M. Becker wrote:
>> Maybe it would be "useful enough" to just restrict to le= ft-hand side:
>
> In my opinion, this is the only reasonable way to implement operator > overloads in PHP.=C2=A0 It is easy to understand, and what can easily = be
> understood is easy to explain, document, and to reason about.=C2=A0 I = do not
> understand why we're talking about commutative operations; even th= e
> inconspicuous plus operator is not commutative in PHP
> (https://3v4l.org/nQcL5).

There are really three different things we shouldn't confuse:

1) Commutativity of the operation, as in $a + $b and $b + $a having the sam= e result. As you say, this is a non-goal; we already have examples of non-c= ommutative operators in PHP, and there are plenty more that have been given= .

2) Commutativity of the *resolution*. This is slightly subtler: if $a and $= b both have implementations of the operator, should $a + $b and $b + $a cal= l the same implementation? We can say "no", but it may be surpris= ing to some users that if $b is a sub-class of $a, its version of + isn'= ;t used by preference.

3) Resolution when *only one side has an implementation*. For instance, how= do you define an overload for 1 / $object? Or for (new DateTime) + (new My= SpecialDateOffset)? It's possible to work around this if the custom cla= ss has to be on the left, but probably not very intuitive.

It's also worth considering that the *resolution* of PHP's operator= s aren't currently determined by their left-hand side, e.g. int + float= and float + int both return a float, which certainly feels like "pref= erring the float implementation regardless of order", even if PHP does= n't technically implement it that way.



How about doing it like in Python,= where there is `__add__` and `__radd__`?

And the = engine could call $op1->add($op2) if $op1 is an object and `add()` is im= plemented, or otherwise call $op2->rightAdd($op1) if $op2 is an object a= nd `rightAdd()` is implemented, or otherwise fail with an error.
=
We could have (distinct) interfaces for both `add()` and `ri= ghtAdd()`.
Or use magic methods like `__add()` and `__rightAdd()`= to allow stricter types instead of `mixed` on the other operand. I think t= here is no extra complexity for the engine by using magic methods or interf= aces.

--=C2=A0
Alex
--0000000000000f55d1062266b884--