Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115666 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 44274 invoked from network); 8 Aug 2021 12:41:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Aug 2021 12:41:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 05B9A1804AA for ; Sun, 8 Aug 2021 06:11:14 -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.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, 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-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 ; Sun, 8 Aug 2021 06:11:13 -0700 (PDT) Received: by mail-wm1-f51.google.com with SMTP id o11so2559104wms.0 for ; Sun, 08 Aug 2021 06:11:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=X/9JfeocMhI0DadM7uUOffNEZwfFutgtVTHsc+vDtTw=; b=pX/en+mbEBc9EGD/cUGq+j+42XdevWrmEUgJudYEsUK98dgv81o0b/xztxxt37yIt2 9lhOVJHJfEXWoVXwgo1eIKqJMi33WC4tJcLaQ16Te3ms75sZhn60J6NzGIJk5e1cugiF MJWQzPz5HWDDfWdr+q0hqiima7rOXCpW4c/831SvzxWbjHRT50Tk+i0Mxwrl712AIW3U Pg7JBxpvkooV9tTbMS6mKHbKgsv1ymNSFV78UGgji32YZLnzjyvaSjeRg7XLbY/1sK20 uoNYR2kKgYnDchFaShKunRd0MTeyyxo3FO0WtkTU45av99XStt+Pr7Np0cq6F5b/iT3Q gmZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=X/9JfeocMhI0DadM7uUOffNEZwfFutgtVTHsc+vDtTw=; b=IXgve/o7gjdhODNi7t67hl/54ZKBJ8Yjca3d7yATgZ5R/qbiLyATpl844AoxikduKq 5BS6aoFI4NxG1sXSLf8PpMa3R5PGeeMaCz9TGdWZXbc4GHafiCCjsoFIFWLM62yyNNKp pQy9uPZUl8CmfN65DNa68TvfgdlOeyDfHyTVyj3GYyvAJTEB3Z3U3Czz8g+Qc9XfoUaY xYUUKrmL1xHURZeaGpye+HMjiwA3ZTNuZsjtdp6gbNcUn5oF521z7yer5ZiqZ4kRbRdC GZgAMbJldZsTGcq+2MQAqrzTvJC8saLdWIsg52aCYrp+NK/MR6BECKQqbEp3jzkM1TKF mEWA== X-Gm-Message-State: AOAM531P0gXZYH9JdCZkh9JiGVttolIL38GVzk1J0S6d3l0KR0mpLL2V b8jfgz2UjaxiE83slILfHVlqn4eHjHA= X-Google-Smtp-Source: ABdhPJxD2VJeADM1EfglttvbXBDlblX+Mpo7G/+N1C4WcuzGGK/TP2f/vsTm7QWFCDhgxljtOJ7Qyg== X-Received: by 2002:a7b:c412:: with SMTP id k18mr11453915wmi.145.1628428269285; Sun, 08 Aug 2021 06:11:09 -0700 (PDT) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id k1sm16431641wrz.61.2021.08.08.06.11.08 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 08 Aug 2021 06:11:08 -0700 (PDT) To: internals@lists.php.net References: <94696d46-c4e6-406a-b859-89144bff31bf@www.fastmail.com> Message-ID: Date: Sun, 8 Aug 2021 14:11:07 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-GB Subject: Re: [PHP-DEV] Revisiting Userland Operator Overloads From: rowan.collins@gmail.com (Rowan Tommins) On 08/08/2021 08:41, Jordan LeDoux wrote: > Off the top of my head here are some of the use cases that I think benefit > greatly from this: > > - Complex number objects > - Fractions objects > - Matrix objects (math) > - Vector objects (math) > - Time/Date intervals > - Collections > - Arbitrary precision numbers (which are usually represented as a string in > PHP) I think that although mathematical libraries feel like a natural example, they're actually rather a small niche in the world of PHP, so not a great advert for the feature. Collections are not necessarily a great example either, since defining "+" to mean "combine these two collections in some type-specific way" is the kind of broad overloading which is often controversial. An example which I think would be applicable to many more PHP users is a "Money" type, with a value, a currency, and perhaps a precision. It's also an interesting example where all the mathematical operators can be implemented, but restricted to certain combinations of types, and even combinations of values. A real-life example from a C# project has the following overloads (syntax tweaked to be more PHP-like): public static operator - (Money $m1, Money $m2): Money public static operator + (Money $m1, Money $m2): Money public static operator / (Money $money, float $divisor): Money // fraction of a Money value public static operator / (Money $m1, Money $m2): float // ratio of two Money values public static operator * (Money $money, float $multiple): Money // note [Money * Money] is not defined public static operator < (Money $m1, Money $m2): bool public static operator > (Money $m1, Money $m2): bool public static operator == (Money $m1, Money $m2): bool public static operator != (Money $m1, Money $m2): bool // defined as !($m1 == $m2) All operators which take two Money values throw an exception if they have different currencies. The [float * Money] case isn't overloaded here, although it would make sense; I don't know if that's because it can't be in C#'s overload system, or just that it is missing from this implementation. Since even a single operator interface can't guarantee that all inputs will have a valid output, I remain unconvinced that implementing 8 different interfaces for the above is any better than implementing one interface and stubbing the parts that have no valid inputs. Regards, -- Rowan Tommins [IMSoP]