Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108623 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 69677 invoked from network); 16 Feb 2020 16:22:42 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Feb 2020 16:22:42 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1E557180504 for ; Sun, 16 Feb 2020 06:37:54 -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.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_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-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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, 16 Feb 2020 06:37:53 -0800 (PST) Received: by mail-wm1-f43.google.com with SMTP id p17so15710018wma.1 for ; Sun, 16 Feb 2020 06:37:53 -0800 (PST) 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=EtdabC0233jKCDCRkt23xH5w2OiiPnlRCmgw9gfPkoA=; b=Hy4U0uwTqZ3Nyh1YC/ijn6kj2tN5PS58ImLeNYwwvT7XmOWhBdwVgy2iJ8phwyqOZz KqZiebfRP53Ro8H9s3Cw8ILXVT/r/Ek7Doct6zRBlCK8mJi3x/EV/+WR4YHmBuh4gRAX haNKyZZDDBBk+PpGzI2fty19s6RwPXKR24GX+sB3d2pqetwEL46cswQw+LF1AX6xvQus PcYzavX/+F1UhPHyydbKuLexDayoA7wmqio4QC+5TVBHgd56ugWj5WM5DqTMYclRnJg6 qiDNaWb9ptexRPwl4HmDRfQC/V00N/At8rkBIips7zayigDZZeFXYvIuWBUEeM6w2Xgl yVMQ== 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=EtdabC0233jKCDCRkt23xH5w2OiiPnlRCmgw9gfPkoA=; b=IKN3Pvv4491UUp5KCNQq9UCL5RHDCVsZUT7Pc+XrQqdbHZZxJ4ujSVb3kVZ8ElchM2 Fxgobt79JEIPUpTgMyTpX6QMVK24Hx1QIj8NymkpCDn+w84eB5fjMD7httea74QTQTxD snMCa3+Gmq2b1Wa9diVl2Tx8mN5LY0I0LODp+zj3g6lQtBiRvZhp2wf/HzfjF/BJO8IY BKwokOCszvf29Re7nw9OVPEaOtlAibasIvuG3gn1wvKLY9E/q2PQKL4vd9Ps0vSw5ppP LE8QxLcGtLYwGheO748gec4NP5qqDqNWA3yEFIQKDK99jfliO/RdXcHADwRvxor/rkD4 3ClA== X-Gm-Message-State: APjAAAUCytsXyhtI33zLhDZOPG5c8QZl0m7CGwVcQt6NVoaEbMkW7bAA NNmd1sxLdNzyUrOX5ek59RDa/2+p X-Google-Smtp-Source: APXvYqzq3IKf+eIk/53qyvSUMFcUwzbd5s5lsLIW1AJZKOLtnYAfvUPj4AoQusts5UaBlkWg17ytdA== X-Received: by 2002:a1c:bdc6:: with SMTP id n189mr17150284wmf.102.1581863871351; Sun, 16 Feb 2020 06:37:51 -0800 (PST) Received: from [192.168.0.14] (cpc84253-brig22-2-0-cust114.3-3.cable.virginm.net. [81.108.141.115]) by smtp.googlemail.com with ESMTPSA id b16sm15815329wmj.39.2020.02.16.06.37.50 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 16 Feb 2020 06:37:50 -0800 (PST) To: 'PHP internals' References: <005b01d5e4d3$4015caa0$c0415fe0$@gmx.de> Message-ID: <222f2c62-66b5-6935-7310-c06f720229ab@gmail.com> Date: Sun, 16 Feb 2020 14:37:48 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <005b01d5e4d3$4015caa0$c0415fe0$@gmx.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] [RFC] Userspace operator overloading From: rowan.collins@gmail.com (Rowan Tommins) On 16/02/2020 14:13, jan.h.boehmer@gmx.de wrote: > Except for simple numbers, almost no mathematical objects define division > (only some special matrices can be on the right hand of a division, and for > vectors there is no definition for division at all). Then my philosophical question would be: are those really the same operation as we have for integers and floats, or are they domain-specific meanings of the symbols? Vectors and matrices, for instance, have multiple types of "product"; does exactly one of them directly correspond to multiplication, or would * simply be overloaded with whichever named operation was most commonly used? > Also think of time > differences: $datetime1 - $datetime2 results in a DateInterval, but > $datetime1 + $datetime2 is not a meaningful operation (Datetime already has > a diff() method that do this). I'm not necessarily saying that the operations need to be symmetrical in allowed types and behaviour, but there is at least a meaning to both addition and subtraction in this case. And notably, DateTime + DateInterval and DateTime - DateInterval *are* a symmetrical pair. > Also it is not really possible to split multiplicative and additive methods > into different behavior, as when $a implements Additive Behavior it should > be possible to do an -$a, but because of the the way PHP compiles the code > this becomes -1*$a. That's a design decision; you could also treat it as equivalent to 0 - $a and call the subtraction overload. > In my opinion this would lead to that even if an object implements these > interfaces, in many cases you cannot be sure that it really supports all > operations, which would contradict the whole idea of defining interfaces. The value I see is not so much about checking the interface and assuming particular behaviour, but encouraging developers to use the feature in a certain way, if there is consensus that the feature is not intended for Domain-Specific Languages. Under the current RFC, I could write this: /**  * Class to represent a Unix command  */ class Command {     // ...     /**      * Implement $command1 | $command2      */     public static function __or(Command $first, Command $second): Command {         return $first->pipeOutputTo($second);     } } If the operations were grouped into interfaces, I would be forced to write this, making it much clearer that I am abusing the feature: /**  * Class to represent a Unix command  */ class Command implements BitwiseOperators {     // ...     /**      * Implement $command1 | $command2      */     public static function bitwiseOr(Command $first, Command $second): Command {         return $first->pipeOutputTo($second);     }     public static function bitwiseAnd(Command $first, Command $second) {         return PHP_OPERAND_TYPES_NOT_SUPPORTED;     }     public static function bitwiseXor(Command $first, Command $second) {         return PHP_OPERAND_TYPES_NOT_SUPPORTED;     }     public static function bitwiseNot(Command $operand) {         return PHP_OPERAND_TYPES_NOT_SUPPORTED;     }     public static function bitwiseShiftLeft(Command $operand, int $i) {         return PHP_OPERAND_TYPES_NOT_SUPPORTED;     }     public static function bitwiseShiftRight(Command $operand, int $i) {         return PHP_OPERAND_TYPES_NOT_SUPPORTED;     } } Regards, -- Rowan Tommins (né Collins) [IMSoP]