Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125932 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 0B5921A00BD for ; Sat, 9 Nov 2024 12:33:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1731155760; bh=abIGBBMuhKPFrjJWH6S89qR6bNsy+5n8vCEpwzOFr5w=; h=Date:From:To:Subject:In-Reply-To:References:From; b=Gs6p+xa36dJ/dbRX4ApBAegBHOT/fJf59Ctmfv54azcYlK540nISqdcnqpG300NMj atRhPvQMwgdTcTv7soWiymtulWtAAuvJxcnQbujgTzNV55m7/zilWDYN0XumnIMzyQ ToSiFQUEHpalTEguoYnRm8u1AoFNh5f+D+4+Gv9Hapfe+5ohBY4sAMrfyzwtCP3fMi zL+qSplH/GrGayJzXKEiHMnINloLEQwTYD1Trmk0AUWISZIPzNwnXwV/3PP5QxfBy8 txTw1+BElNtg6zjq0ziQjLDx1Hn5NNv2H5AZ5DAbdQiWt+Ocf612A2gE/HV9oJ2U7U cGvgU2/MNLpGA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9787D180037 for ; Sat, 9 Nov 2024 12:35:59 +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,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-b3-smtp.messagingengine.com (fhigh-b3-smtp.messagingengine.com [202.12.124.154]) (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 ; Sat, 9 Nov 2024 12:35:58 +0000 (UTC) Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfhigh.stl.internal (Postfix) with ESMTP id D9CFC2540125 for ; Sat, 9 Nov 2024 07:33:24 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Sat, 09 Nov 2024 07:33:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-transfer-encoding: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=fm3; t=1731155604; x=1731242004; bh=abIGBBMuhKPFrjJWH6S89qR6bNsy+5n8vCEpwzOFr5w=; b= rC9k/bmqMdkjKdfaGhTu1olvi2jate/iu8Es6a9gx6tMC2gaq63ILzvwiGBbTWNE zm8yuK+jeAoqpizxVRGML01hRm0rau3YDV3e3tEx7U9SzE6zf9UE0OH4GLWzZZrj Fsx8acCagLRRmwYEMnIBGJlVihFUeUdwOfaSeorECDNRuRq1IeS8e/HslD59qa1Q P/DP91/aV+LjHHc6O6bNzCEsD4wJHgkoitR9/J2pzqTiefAcpjxwArG6Kcrxg+Lo wd6OU6KrCCknFfq9ri5H0jUWZSowfZUvhT09YamGe7sgJrz6lxs3QKoK9Vn2kMHv cltOkxFpTVKhFXLJlW4j+w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding: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-sender :x-me-sender:x-sasl-enc; s=fm3; t=1731155604; x=1731242004; bh=a bIGBBMuhKPFrjJWH6S89qR6bNsy+5n8vCEpwzOFr5w=; b=lESz/8ukpqd66Cu32 3EnwktU9ger+fEU9zivicK/4rK1yqV+mBIetz1Aqtw98FPxLDtM42RK1SOauJp24 qYsnrlcRoAOtAh7LlpzlkdtpC2yFkoeSoLsRZ2eGJPLgZGREI70g15cizgUFEa0t On6CA7HunfM323OFwzk1cz4UsttsNuzPaMV0YjkUw8AuyvcPxUGkjo70OHptfWqy Z/PARJFbytYja5PixaUDnJ19ubcwoFRoyaJXy2pyuagc93+76IADcW/o9p5ebRE3 pDyT1kLU6Vqc7v/ksdToLY66ADRcoi01DUo5jeJHsupJsWDlbLvpjaKXPr7IF9iN BOH/w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrtdekgdegudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecunecujfgurhepfffhvffufg gjfhfkgggtgfesthhqmhdttderjeenucfhrhhomhepfdftohifrghnucfvohhmmhhinhhs ucglkffoufhorfgnfdcuoehimhhsohhprdhphhhpsehrfigvtgdrtghordhukheqnecugg ftrfgrthhtvghrnhepheelffetiefgveduteefudegtdduveeludegueegleehiefhhefg tdekveevgfelnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrh homhepihhmshhophdrphhhphesrhifvggtrdgtohdruhhkpdhnsggprhgtphhtthhopedu pdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlhhssehlihhsth hsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Sat, 9 Nov 2024 07:33:23 -0500 (EST) Date: Sat, 09 Nov 2024 12:33:22 +0000 To: internals@lists.php.net Subject: Re: [PHP-DEV] First-class types RFC User-Agent: K-9 Mail for Android In-Reply-To: References: Message-ID: Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On 9 November 2024 08:39:57 GMT, Adam Kecskes wrote: >I propose adding first-class types to PHP, as this feature could greatly = enhance the language, especially given PHP's unique runtime type-checking s= ystem=2E Many libraries currently rely on class strings as function paramet= ers, which could be made significantly safer with native types rather than = strings=2E Hi Adam,=20 I think this is an interesting idea to think about, although I have no ide= a what the implementation might look like=2E One of the things I've been thinking about for some time is how to improve= PHP's type casting functionality, and a key sticking point is a sufficient= ly flexible syntax=2E Not wanting to get into the casting itself, let's ima= gine we want to add "foo casts", available for all types, including unions,= nullable types, etc; options seem to be:=20 - foo_cast("int|string", $value) - a "stringly typed" function with no new= syntax; flexible, but ugly and awkward to statically analyse=20 - foo_cast($value as int|string) - a new keyword for each type of cast; ex= pressive, but a high barrier to adding more casting options - foo_cast($value) - if we add generics, even without type inf= erence, that syntax could be used to "specialise" by type - foo_cast(int|string, $value) - passing the type as a parameter would rea= d very nicely There's a lot of overlap between the last two, but also some differences: - generics give a clear way of expressing that the function will return th= e given type, or perhaps some manipulation of it, like "function foo_cast(mixed $value): T|CastError" - first-class types, on the other hand, give a clear way for userland to p= ass around type specifications, like "function cast_in_turn(mixed $value, t= ype =2E=2E=2E$types): array { /* loop over $types, calling foo_cast($type, = $value) for each one */ }" Would first-class types and generics need to be mutually exclusive, to avo= id a confusing overlap of functionality? Or could they be made explicitly c= omplementary somehow? I wonder what prior art there is in this area, in both the theory of type = systems, and the practice of language implementations? Regards, Rowan Tommins [IMSoP]