Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116682 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 18904 invoked from network); 19 Dec 2021 16:48:30 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Dec 2021 16:48:30 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BEFD01804A8 for ; Sun, 19 Dec 2021 09:51:45 -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=-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-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (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, 19 Dec 2021 09:51:45 -0800 (PST) Received: by mail-wr1-f54.google.com with SMTP id q16so15385284wrg.7 for ; Sun, 19 Dec 2021 09:51:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=mqDYR/Tgi3C9ND9yJYW/1lsrGveJSST824BvLQRuEec=; b=di0cyrjaXSbnY/GQD9H2YFceJA+YiE0wj/NbSfFujjGtPLMzjlHsrrDvgvceIvD4we HKuF5MD/HL6RygTtNnaS1L4X1uU39IcTRoUg2g45gnGgTv8WuSsWowJp+s9UUN0eMCnR KeqPs8swfiO0/TjM2EVjp4TOM5OTK5ZpOT2Jl/1IWkAczqiJhLjitLPBtdLR2bN5wCBP jwWelhoXZlugiau46iV+6Amsp2one5VxH4ExPFq0fujFhpzL84lMP4u5dcPjsjQ5ADvB KPVGYY85pj6gdiKqOZPCwze12geFpL3glUHM6pzRMfX1N0eQbpUDiDO+f2NTdI3vlq4P rcIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=mqDYR/Tgi3C9ND9yJYW/1lsrGveJSST824BvLQRuEec=; b=26ziFeKH0gEl8F83ZAjSyOnegt3+A7wM7pJPKdcQ2DpjIxyy4/Lb6ONUMc7kS3MRXn lh6BTcZe5Zf3rXcguAv1Gli9ksJDlvwNEOxgX5yTaX7MvhUZBzEUfbEqPPUbIhZjvkBW ekIqHjbQkQ+a50MDQkZH/ffY7b6Xik9fywOoiPPLtjIK4w1szUNT2h8UlcyGRl8QC+Iq WiVBT1NROes09gmwvbdg+HhZt0v8A2ffFd8I2kMLyV66SmZ6MXzpCVCS8WQfi9QZ076y dzjBKbrjtplXbIkvvAIAAPl9PXDPp5PbQ/rBwgj8dqAFI+mmNYRghf5F93Tsarycmw+a 1Y1g== X-Gm-Message-State: AOAM5327NucID2/7jiucZ8hTwJIGRZOGe4juM3j7+Gy6AYnuX3uiokFS EFWV+CGWVMHbeiO6PqbnkkbuF3OpM3c= X-Google-Smtp-Source: ABdhPJxqjS1qpd3ki8Y1fQTNdbu0iOE913lcNHiU9ZYLhdP0xUce05cSUIKKrLrTI1Lbg66mRsjEOw== X-Received: by 2002:adf:eb4c:: with SMTP id u12mr10061089wrn.707.1639936304111; Sun, 19 Dec 2021 09:51:44 -0800 (PST) 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 i17sm14794731wmq.48.2021.12.19.09.51.43 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 19 Dec 2021 09:51:43 -0800 (PST) Message-ID: Date: Sun, 19 Dec 2021 17:51:42 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Content-Language: en-GB To: internals@lists.php.net References: <44b3fb4b-4693-1639-c8c0-5e17296c196e@gmail.com> <4b58c011-ed87-ba87-201d-0cf8e4116c6f@processus.org> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] [RFC] User Defined Operator Overloads (v0.6) From: rowan.collins@gmail.com (Rowan Tommins) On 17/12/2021 22:04, Jordan LeDoux wrote: > In general, unioning types should be seen as a "code smell" with this > feature in my personal opinion. If you start to see 4, 5, 6 different types > in your parameters, it should be a signal that you want to re-examine how > you are implementing them. I think it works well for this purpose, as many > developers already try to refactor code which has very complicated type > unions. I'm not sure this argument really makes sense in context, because the usual way to refactor a method with a lot of unioned types would be to create multiple methods with different names; with operator overloads, you clearly can't do that. In one of the previous discussions, I shared a real life C# Money example: https://externals.io/message/115648#115666 I thought it would be interesting to see how that would look in the current proposal. Most of the operators are straight-forward: public operator - (Money $other, OperandPosition $operandPos): Money public operator + (Money $other, OperandPosition $operandPos): Money public operator * (float $multiple, OperandPosition $operandPos): Money public operator == (Money $other, OperandPosition $operandPos): bool public operator <=> (Money $other, OperandPosition $operandPos): int The division cases however are a little awkward: /**  * @param float|Money $divisor A float to calculate a fraction, or another Money to calculate a ratio  * @return Money|float Money if $divisor is float, float if $divisor is Money  * @throws TypeError if $divisor is float, and OperandPosition is OperandPosition::RightSide  */ public operator / (float|Money $divisor, OperandPosition $operandPos): Money|float The intent is to support Money / float returning Money, and Money / Money returning float, but not float / Money. I don't think this kind of type list would be unusual, but it may be a compromise we have to live with given PHP's type system. Regards, -- Rowan Tommins [IMSoP]