Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116674 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 90550 invoked from network); 17 Dec 2021 16:40:55 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Dec 2021 16:40:55 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 63ECD180510 for ; Fri, 17 Dec 2021 09:43:40 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_20,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 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-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 17 Dec 2021 09:43:37 -0800 (PST) Received: by mail-lj1-f182.google.com with SMTP id k23so4570777lje.1 for ; Fri, 17 Dec 2021 09:43:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Xcya6tdd0AGJZeSQUiHDP/Laxfol2Cjtlp5zIdy4ivw=; b=QR2ncPXTQxvIL1+ocFQvTDI4CyBM2C28U0SO6Ii0jiyna5exGiH5Wvt+t2TeLbQMWA 8rsIZ8PnQEu7ngxUfLaktiB+LFkO+AhveknJ9WKgPXlbCCM7HoFpQpktOsNVrjeZVsu6 SZAmt9Zkds/d22OIS42tVX7XLnASLtcSzXLIEk08bmznmrdzQHrKDb7U3VhyoETB2hLA ZJsKrBns2z1qqQ7U4XAiCYeINTMM99AlykI5uvBM3CXPTP3OpqZbp6vQCtW/jwG7xeYS iW6vCxvrMOYq3tn2CebPU+4t3I5FuljYPUW/V6T4CT60H5RIEpaH8VZiD1n/Fju7vGzb cSRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Xcya6tdd0AGJZeSQUiHDP/Laxfol2Cjtlp5zIdy4ivw=; b=tTUelhKIg+I8MZXSifBZvd2DSA09/Wr8+1bzKypBw8zgTJM2WvAbr7218gNVkA5v31 nV4vZS8Gkvrt7bEjfoNiT2GVLXejSymTbhOX6YwfU+WMGeKh0zd1puGV1LuLfL8du0f4 97TgUB6KV8M7+2aT6HJL0PTMBrHYQ8z7IoKCm0oS8RudSp2dZL9+fLyLOQTgUaTu+q7R byeB6on9M27LajgJEjCo+BteW+pK/C2cLmc4wQlfAqKE9+yvSrkg/lKE3Qq2FwLV04Uw Z0VbAJ/2/YGhBr+IpFXKVKW6GtoGMTgUSxJIzUY5UfXtL+4L1hCiZWZWsUl3P+tPliBX 1PPA== X-Gm-Message-State: AOAM532LgulXKHYulonbMIsWY0kG61t8NWFQwTmd726SQrnoTIdMWW3/ 5P6kt7I4qnlaN9Su3ztwQIVtduxdqrvZosPxeFGE+JXM X-Google-Smtp-Source: ABdhPJwMOBjDAyM8NDdAiadKhV1o7P5EnnduEBF/R6UFPx1wbAriqUvTOGoiyfo4K21E9kn+TZHozwpEMNBQOvAk79s= X-Received: by 2002:a2e:9196:: with SMTP id f22mr3572857ljg.444.1639763015499; Fri, 17 Dec 2021 09:43:35 -0800 (PST) MIME-Version: 1.0 References: <44b3fb4b-4693-1639-c8c0-5e17296c196e@gmail.com> <4b58c011-ed87-ba87-201d-0cf8e4116c6f@processus.org> In-Reply-To: Date: Fri, 17 Dec 2021 09:43:09 -0800 Message-ID: To: Jordan LeDoux Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000000e026205d35b1473" Subject: Re: [PHP-DEV] [RFC] User Defined Operator Overloads (v0.6) From: matthewfonda@gmail.com (Matt Fonda) --0000000000000e026205d35b1473 Content-Type: text/plain; charset="UTF-8" Hi Jordan, Thanks for the RFC. I have a couple questions: Suppose I have classes Foo and Bar, and I want to support the following operations: - Foo * Bar (returns Foo) - Bar * Foo (returns Foo) If I understand correctly, there are three possible ways I could implement this: a) Implement the * operator in Foo, accepting a Foo|Bar, and use the OperandPosition to determine if I am doing Foo * Bar or Bar * Foo and implement the necessary logic accordingly. b) Implement the * operator in Bar, accepting a Foo|Bar, and use the OperandPosition to determine if I am doing Foo * Bar or Bar * Foo and implement the necessary logic accordingly. c) Implement the * operator in Foo, accepting a Bar (handles Foo * Bar side); Implement the * operator in Bar, accepting a Foo (handles Bar * Foo side) Is this understanding correct? If so, which is the preferred approach and why? If not, can you clarify the best way to accomplish this? Next, suppose I also want to support int * Foo (returns int). To do this, I must implement * in Foo, which would look like one of the following (depending on which approach above) public operator *(Foo|int $other, OperandPos $pos): Foo|int { ... } public operator *(Foo|Bar|int $other, OperandPos $pos): Foo|int { ... } Now, suppose I have an operation like `42 * $foo`, which as described above, should return int. It seems it is not possible to enforce this via typing, is that correct? i.e. every time I use this, I am forced to do: $result = 42 * $foo; if (is_int($result)) { // can't just assume it's an int because * returns Foo|int } Thanks, --Matt --0000000000000e026205d35b1473--