Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109579 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 62132 invoked from network); 9 Apr 2020 13:50:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Apr 2020 13:50:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 51D871804C3 for ; Thu, 9 Apr 2020 05:18:43 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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-vs1-f41.google.com (mail-vs1-f41.google.com [209.85.217.41]) (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 ; Thu, 9 Apr 2020 05:18:42 -0700 (PDT) Received: by mail-vs1-f41.google.com with SMTP id s25so4802701vsp.11 for ; Thu, 09 Apr 2020 05:18:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=basereality-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=NR5SiMT+Jp/zzKVC/G9P1TMKAL8rO+2gtlQi6reVDfY=; b=JagWeKH/t+0wqECS7iAFJEj3ttu6XBVp1sHqAGsoNeUyNz9B2IPQanXltMNpmnYvOH qGojDHJk60+Bw7IWzAXm6wa0ZW2lsJOGK4+xL8noTyiKnI+2vLNRYIrpQY63dv6FjSY8 We6vnLoq/sxOX4MwXoso4PE2XEPCUq1npPJlAPI3KBXoSK4gBZF4QTIPagv/f3UC3QOl PPj7epf+OBm/nNdfglB8V5pzjMom4pxe/HdEcIzwwnvINJMrD7E73/28dRv2cvYCqMa6 42QUUR0iIb0N2y77MPtSzsl9O79mKVvQ+HuHkY177x19uJ7BOG8UNPSCRPjopdkp/59g 9lZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=NR5SiMT+Jp/zzKVC/G9P1TMKAL8rO+2gtlQi6reVDfY=; b=O0Pe+QQOx2Voe1nAmxyZMx19bhVHGtIqRlDHAp+EwO3TZtiVZlGk8JsWZHYUtl1hbX qi55108cplQjvIPifKij//3RoCHdyX7jDhX3oVjwkhxMuPg1HtL4tAXZCmmCVnDLRuW9 qIAMgiA/+99m1QeRhnDZbrNZfe8Ha3YcKix++AFXxLbBj8g4AbO1xG7/RiJ7zKhNPeIG FiOI12yozKNVYuIQ6ck04f9wB80xRNhKG8YMjhFvSN0wzQbMHKhAoxO02jA5aYoneCaT /1BaE2hYgEcA/WfynzO4fmAxU9xzjcuoppCraupVZ5fp0MxvfHr1nEW2BUd8Jnsv5swj xpoA== X-Gm-Message-State: AGi0PuYLQ25q97A7WPWfxfBi/nJT3T2MkpA8G/UnhJKQkKwYavEVlnH+ n/ADt5IBLgdIhmLaWV/SWrp7jQ9mYwRWtLcdFkpTng== X-Google-Smtp-Source: APiQypKA3COyJrXoORmHM5soT+7IoMcYiW+xTsqi60oCURqdpXMpjfb4ECqMjANJNN6RysqJkSew64YP5s8JSMvyGV4= X-Received: by 2002:a05:6102:30b5:: with SMTP id y21mr2261484vsd.63.1586434718768; Thu, 09 Apr 2020 05:18:38 -0700 (PDT) MIME-Version: 1.0 References: <003701d6013c$9afe9750$d0fbc5f0$@gmx.de> <7a83f950a31d94d5ff2307ac8219db3b7b6482b6.camel@schlueters.de> <12ad7c71-8958-7742-12c4-e83e359c8186@gmx.de> <3B71F74D-8142-48FB-9660-835B08D1DDDD@schlueters.de> <705aba69-8c17-f882-19fd-6f41a2c2ca25@gmx.de> <07f176a5c2ff0338cb67c9755bf37af6dcc2d465.camel@schlueters.de> <000d01d60c4a$a213fb20$e63bf160$@gmx.de> In-Reply-To: <000d01d60c4a$a213fb20$e63bf160$@gmx.de> Date: Thu, 9 Apr 2020 13:18:27 +0100 Message-ID: To: jan.h.boehmer@gmx.de Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [VOTE] Userspace operator overloading From: Danack@basereality.com (Dan Ackroyd) On Mon, 6 Apr 2020 at 20:36, wrote: > > Hi internals, > > I have closed the voting. With 38 in favor and 28 against the RFC is DECL= INED (didn=E2=80=99t reach the needed 2/3 majority for a new feature). > > Thanks to everyone who has participated. > Hi Jan, Thanks for running the RFC. Although it didn't quite pass it seems a lot closer now. Apologies for not taking part in the discussion earlier, but I'm slow at thinking. To follow up on a couple of things. > If it can not handle the given type, it has to return the > constant PHP_OPERAND_TYPES_NOT_SUPPORTED (currently just null). This does not appear to be a good choice. It means that to evaluate whether the code is safe to call the code needs to be evaluated. That makes doing static code analysis very difficult. Also, it means that operators could not return null as a result of the operation, which seems like an odd choice that would be a regretful limitation. Rowan Tommins wrote from https://externals.io/message/108788#108993 : > > > $a =3D new A; > $b =3D new B; > var_dump($b + $a); # calls B::__add($b, $a); OK > var_dump($a + $b); # calls A::__add($a, $b), which is a TypeError And jan.h.boehmer@gmx.de wrote: > > If an operator handler has typehints, an error will be thrown, > What do others think about this restriction? I think that is the wrong solution to the wrong problem. It's a wrong solution because parameter types* are a useful tool both for program correctness at runtime, they make it easier to run static code analysis tools on the code and most importantly they save a lot of developer time, mostly through autocomplete. It's the wrong problem because that code does have a TypeError. It is calling '__add' and passing a parameter to the method that the code can't handle. It appears to be the same error case as: ``` class A { public function add(A $lhs, A $rhs) {...} } class B { public function add(A|B $lhs, A|B $rhs) {...} } $a =3D new A; $b =3D new B; $b->add($a); // Ok $a->add($b); // TypeError ``` For me, the solution to the error in this code is not "remove the parameter type on A::add". I'm pretty sure that the solution to this type of error would be "fix your code". When someone looks at the idea of userspace operator overloading again, I think the following path might reach a more acceptable solution. * Encourage people to use parameter types to make their code easier to reason about. * Encourage people to use static analysis to check their code for errors, including errors where they have invalid operands to operations. * Take the approach in the RFC that passing a wrong parameter type to an operator magic method represents a programming error that cannot be handled in the normal program flow, and that throwing an exception is the right thing to do here**. But as most people should be using parameter types, and static analyzers to check for this type of error, this should not occur. Other than a learned reticence against throwing exceptions, what downsides would that have? cheers Dan Ack * PHP has parameter types that are checked at runtime, not 'hints' that can be worked around as found in other programming languages. If people would stop using the phrase 'type hint', it would make the conversation more accurate. ** This is not using exceptions for flow control (though it could be abused for it). Most people should be avoiding these problems through writing code correctly, and using static analysis to double-check their code is free from errors.