Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125488 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 97AC81A00BD for ; Tue, 10 Sep 2024 08:32:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1725957264; bh=b5FDO8tHwFL/xM3ZCoJ7IeQaeVExr/OTofYsorsYdW4=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From; b=fq0WDWql2KKXvHWWkE2i/PABWEt6mozo2HmYbvwKiScL7N/SIIKxHUU/zcwU9qUuQ jU3ntqwfaSm+brKIB7uhhSIPNNaVeJdoEJuz71uWwDAQxzBjvlnY8OMrX+4fVTWTmu 1CdUHZItJw6D2bfC++oQG8bTMnunohC/rjBg+0LYJWsMSa1RzdU3tUoPlXhQgiUtZX EMV6tLkArrca2CW/YWuW7v+Cm0Cd4tJiVItkCfsoExf86GeIBWGmvr5L8x0pbQnp6s 9p7Nv+hhpttgn39SfXpjGCXGDL6V0RLRWevbdSiNroctaTOEvQTlEEwZEVla4QACPY d7REmEYtnuiFQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2BBB1180052 for ; Tue, 10 Sep 2024 08:34:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh7-smtp.messagingengine.com (fhigh7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 10 Sep 2024 08:34:22 +0000 (UTC) Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfhigh.phl.internal (Postfix) with ESMTP id CE2E01140100; Tue, 10 Sep 2024 04:32:20 -0400 (EDT) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-01.internal (MEProxy); Tue, 10 Sep 2024 04:32:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:cc:content-type:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1725957140; x= 1726043540; bh=heMaQEWAMjpDvjXg6EdPPZyN6ENdzMlBi7Oa4JApd2s=; b=S V91+yuikFnZ8T0iUOw7yAW6UNKeN0IgWz7xT+XKvnPKLDQO44AfnP6Wx2EgUnXhJ aYjIpM2DDZlwAa7/ehlj/x0ZKFRahbZ/ItgcCL7e1wNjjX9DBlKU3ajeke8rJeAV eK+XKMS1uLv1R1LciQ8s2gfMLMonfQbfIFVUdRh1s+dTNA2BpANkAlsNQsKKgr9/ pDNk3iaVvIwEEEhuvssBMVdQ5v2hzG+exX/0QfRMqagi5TYi1OJTPNtG1p+lfmSK Yhm7jbRZMysq0MdG7DZWmNRGbYE/lByeYAtEDMfyztBObZB73mh/iIexNk3H4KUW 7RsjYHmOXpkHwKN9ZSUvg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1725957140; x=1726043540; bh=heMaQEWAMjpDvjXg6EdPPZyN6ENd zMlBi7Oa4JApd2s=; b=lM/QDsKSjrA7lLILV0FLJhAY0IHnXWQGerCFMEpk03iG LZmBlrJ4JinBD2GvaDjct36XL0r4AxKDLjnv5sKEsp5YZiyCaISxCUouA7VS0yC3 p3EI7ZQRZbS888RqPaeg5C4yWYuz6pvUehG3L2Zo650yCss64VGTjbObgxltHkOW dxc5NBeCGoH1sPmA7dzpF3OrkCYQjMDjrqAWKsE/7TctqEc358fBtIMddpidmhlP 9UPVHFUvN4hF23+t1I5P9spN6Tm5BcOS7nsy/At3AXNRIfNpZV94MvG57Nj10P38 RcZlTGSG3zF2a3Wjj+uqk17FW9Fcxm8DSvRHBjh/yg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudeiledgtdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepofggfffhvfevkfgjfhfutgesrgdtreerredtjeen ucfhrhhomhepfdftohgsucfnrghnuggvrhhsfdcuoehrohgssegsohhtthhlvggurdgtoh guvghsqeenucggtffrrghtthgvrhhnpeeiueethedvvdefjefhgfeiheelheehtdfhfeek jefflefgvedvkeduteejjedttdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpehrohgssegsohhtthhlvggurdgtohguvghspdhnsggprhgtphht thhopeefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlhhsse hlihhsthhsrdhphhhprdhnvghtpdhrtghpthhtohepmhhikhgvsehnvgiftghlrghrihht hidrnhgvthdprhgtphhtthhopehimhhsohhprdhphhhpsehrfigvtgdrtghordhukh X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 64412780067; Tue, 10 Sep 2024 04:32:20 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Tue, 10 Sep 2024 10:31:59 +0200 To: "Mike Schinkel" , "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Message-ID: In-Reply-To: <50CFF539-AD22-4F89-A65E-77AF76DBD63A@newclarity.net> References: <0fa39535-f22d-4eba-b4df-90abe39e683a@app.fastmail.com> <79e58673-50ec-461e-a998-736b020e4287@app.fastmail.com> <928A2984-6035-4DA6-9EA7-12E85237C270@php.net> <0d461700-1b6c-44fd-9cda-aa698de49847@app.fastmail.com> <667233C2-BC47-4530-8142-D90E6907FE63@daveyshafik.com> <63d241a8-a34a-498c-a5f9-f34230aa5afa@app.fastmail.com> <4C7A7F27-B787-44CA-B664-CEF4B9B412FB@newclarity.net> <212fd466-85bc-4447-90b7-8fe5426d1bd1@rwec.co.uk> <50CFF539-AD22-4F89-A65E-77AF76DBD63A@newclarity.net> Subject: Re: [PHP-DEV] bikeshed: Typed Aliases Content-Type: multipart/alternative; boundary=5d1189d20ecf416682d85417012414b4 From: rob@bottled.codes ("Rob Landers") --5d1189d20ecf416682d85417012414b4 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Tue, Sep 10, 2024, at 10:00, Mike Schinkel wrote: >> On Sep 9, 2024, at 5:35 PM, Rowan Tommins [IMSoP] wrote: >>=20 >> On 09/09/2024 19:41, Mike Schinkel wrote: >>> In Go you cannot add or subtract on a typedef without casting to the=20 >>> underlying type. I would definitely prefer that to be relaxed, but = only >>> if it is relaxed via an explicit opt-in, e.g. something maybe like=20 >>> this: >>>=20 >>> typedef UserId: int operations: +, -, *, /; >>> typedef UserName: string operations: .; >> I think this would stray into some of the same complexity as operator= overloads on objects, in terms of the types and values allowed. For ins= tance: >>=20 > I tend to agree that allowing operations may be too much for an initia= l scope given that it is unlike anything else in the current language an= d with no other languages offering an equivalent AFAIK. >=20 > I would however make the distinction that it is unlike operator overlo= ading because the big concern was what constituted an operation for any = given type could be too subjective. In your example of `Metres` it is p= retty obvious, but not at all obvious for a `User`, for example. (BTW, = thank you for not calling out my nonsensical example of operations on a = `UserId`; when I wrote that I clear was not thinking about if they were = relevant, doh!) >=20 > However give the suggestion regarding operations with a typedef, the o= nly operations that I suggested would be valid would be the ones already= defined on the underlying type, (when I mentioned other operations I wa= s thinking of methods =E2=80=94 see my the following example with round = =E2=80=94 not operators so that is not the same as operator overload.) F= or example: >=20 > /** > * Currency is an int so for example in USD 1=20 > * unit of currency not a dollar but a cent. > */ > typedef Currency: int operations: +,-,*,/,round; > function CalcTotal(Currency $subTotal, Currency $shipping, float $tax)= :Currency { > return round($subTotal*(1+$tax/100),0) + $shipping; > } This is very similar (behaviorally) to what I wanted to do with GMP. Ove= rloading GMP would have given you int-like powers in your type. The bigg= est negative feedback I got was that people would abuse it still; so we = need some way to prevent abuse. If you read Jordon's operator overloads = RFC, and my GMP overloading RFC, you can see that users basically need a= way to define how to operate over even just an integer. For example, Dollar(1) + Euro(3) is what? Or even Dollar(1) + 1? How doe= s a developer prevent someone from doing something nonsensical? The lang= uage needs to enforce some rules and/or the developer needs to be able t= o define them. These rules need to be intuitive and well reasoned, IMHO. >> typedef Metres: int; >>=20 >> assert( Metres(2) + Metres(1) =3D=3D=3D Metres(3) ); // most obvious >> assert( Metres(2) + 1 =3D=3D=3D Metres(3) ); // seems pretty clear >=20 > Both of those are in line with what I was suggesting. >>=20 >>=20 >> $_GET['input'] =3D '1'; >> assert( Metres(2) + $_GET['input'] =3D=3D=3D Metres(3) ); // might be= more controversial >>=20 >>=20 > I would not consider this appropriate as it has two levels of conversi= on and could thus end up with unintended edge cases. To do the above I t= hink you would have to either convert or typecast: >=20 > assert( Metres(2) + intval($_GET['input']) =3D=3D=3D Metres(3) );=20 > assert( Metres(2) + (int)$_GET['input'] =3D=3D=3D Metres(3) );=20 >>=20 >>=20 >> typedef Feet: int; >> assert( Metres(2) + Feet(1) =3D=3D=3D Metres(3) ); // almost certainl= y a bad idea >>=20 >>=20 > This would be operator overloading where knowledge of the conversion b= etween meters and feet would be required, and that is not in any way in = scope with what I was suggesting. =20 >=20 > As an aside, I am against userland operator overloading as I have seen= in other languages that operator overloading gets abused and results in= code that is a nightmare to maintain. OTOH, I would support operator ov= erloading in specific cases, e.g. a `Measurement` class in PHP core coul= d allow adding meters to feet, assuming such a proposal were made and al= l other aspects of the RFC were of the nature to be voted in. >=20 > To reiterate on typedefs, what I was suggesting was that if an operati= on was explicitly allowed =E2=80=94 e.g. + =E2=80=94 then anything that = would work with the underlying type =E2=80=94 such as adding an int 1 wo= uld work without typecasting and yet still result in the typedef type, e= .g. Meters(2) + 1 results in a value of type Meters. (note that I correc= ted your spelling of 'Meters' here. ;-)=20 >=20 > But I agree, this is probably a bridge too far for a first RFC for typ= edefs.=20 >=20 >>> type MyNewType: Foo >>> type MyAlias =3D Foo >> I know this was only an example, but as a general point, I think we s= hould avoid concise but cryptic differences like this. PHP is generally = keyword-heavy, rather than punctuation-heavy, and I think that's a valid= style which we should keep to. >>=20 > Here, I also tend to agree WRT PHP. Was just pointing out for sake of= laying out other options that were implied not to exist. >=20 > -Mike In other news, I'm highly considering refactoring the records RFC to be = a typedef RFC; the infrastructure is there; we just need more restrictio= ns. =E2=80=94 Rob --5d1189d20ecf416682d85417012414b4 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable