Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127170 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 058A91AEA82 for ; Tue, 22 Apr 2025 20:38:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1745354153; bh=I2eSHsLFHxl6qJOKN/Ukwb2ur241Fqb2xuzkdBf8nWI=; h=Date:From:To:In-Reply-To:References:Subject:From; b=MVPJUqKxWiPn2t6Dm/Xb6lGt2zSR/zv53Wl1Do8hdntJsr50k5wC6SmR7hMA3a3vz WUYwxS2yNXsEhNQNX502uQjvVKvfNxdVIdMR8JP1wmhFItuKxE/lbV7qYc2B8rQafW Q/MB0V/XNn0rTG06TiOeFd9rgGMjVcYd9rHxIw33x6qQd2Agy1vMC4mJr5OcRUFzg/ xmFBHmwiLE1nnyVThBim/4u/L4EpsS2Z8BQe/6QB/K2kEr2gxRAFx/ccKfzP2UStSn yjvJDoaFi0+r/6Lu/x5h+ZKhQOo/YuwWYu4dJQZ6rXwZwPp54zJpPlCzgsTFjVpyzA 79jnkJ5LEXZsg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 42F7A18007E for ; Tue, 22 Apr 2025 20:35:50 +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=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from fout-b8-smtp.messagingengine.com (fout-b8-smtp.messagingengine.com [202.12.124.151]) (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, 22 Apr 2025 20:34:33 +0000 (UTC) Received: from phl-compute-10.internal (phl-compute-10.phl.internal [10.202.2.50]) by mailfout.stl.internal (Postfix) with ESMTP id E8CD71140157 for ; Tue, 22 Apr 2025 16:36:51 -0400 (EDT) Received: from phl-imap-06 ([10.202.2.83]) by phl-compute-10.internal (MEProxy); Tue, 22 Apr 2025 16:36:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; 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=fm2; t=1745354211; x=1745440611; bh=kIffKxtbQFH2o92cpCQim UmfFKYCZiJhvYQf0Of5V6M=; b=xAGT0f6LmTN4tko2DbvwVlYnFhj/xPgHwvz3F VIRtNJl5vzJbr5ONH/KqLLzXBL4NOSsztC++lOrM+mRDtwK3rWncOvFgAfQgvoD/ 06qrdOoEoaz926F5fc3FqCeZVxv7zGcLYqbMh+KFypcKKmAizAMG4KFKHpBjBHfv ADUSneeP6EzbsZCOKWPXN4jTw5M9LIE6euuwuuCuFGyLGyDwl42JT+5LLrfDsnnD wqyNtWaq82apwAnDY8MYvoQfknlCGAeGsxAuHukKTn+jSTO75pCsOAMjQIFfmj8c /bU+K9CH8a9DXcI7Cim3Fcl+s3NWBj3mgneLnKhT3naFvus8A== 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=fm2; t=1745354211; x=1745440611; bh=k IffKxtbQFH2o92cpCQimUmfFKYCZiJhvYQf0Of5V6M=; b=roHjFs7xXQgrPPne3 5oftDeNT7SAfb+MUI5L6NcNnjdIDk1o9XXSn+vfK7oyoEhV/ZSQgOh3BVTQ/7/Dg BvBVp3BdRFR1xz5LKFmnusxIRyQb85trQ4weJIC6yps27Xc1EHNIAsGFJSc8KpX8 cdsbn5tXuxRsxdqllQ9Iddj3N2eqGSNINVBWN8/rJhhy9ErZtLg1/Z4G8Yqfs+3V mZYhF42h5/Ji3K+Co9W3vAlrjJT2/MhLbalomVH/mIWmtiX8QkNjPJndLbXZAbs3 afqH2Bn7MMrh9zQObggT4IX7TB3jZwIlnbqNF7Bx9QvvJPucD1vaS0CCnEA9gr0J HtZzg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvgeegjeduucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpih gvnhhtshculddquddttddmnecujfgurhepofggfffhvffkjghfufgtgfesthejredtredt tdenucfhrhhomhepfdfnrghrrhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrh hfihgvlhguthgvtghhrdgtohhmqeenucggtffrrghtthgvrhhnpefgueeiieelffevheej vdevkeehudehfeetheevheevudetvdehtddvueegheeutdenucffohhmrghinhepthhhvg hphhhprdhfohhunhgurghtihhonhdpghhithhhuhgsrdgtohhmpdhphhhprdhnvghtnecu vehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhrhi esghgrrhhfihgvlhguthgvtghhrdgtohhmpdhnsggprhgtphhtthhopedupdhmohguvgep shhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprd hnvght X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 7A1E329C0072; Tue, 22 Apr 2025 16:36:51 -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 X-ThreadId: Tdaa8b3b0fb7b52d4 Date: Tue, 22 Apr 2025 15:36:31 -0500 To: "php internals" Message-ID: In-Reply-To: References: Subject: Re: [PHP-DEV][Pre-RFC] Associated Types Content-Type: text/plain Content-Transfer-Encoding: 7bit From: larry@garfieldtech.com ("Larry Garfield") On Tue, Apr 22, 2025, at 8:37 AM, Gina P. Banyard wrote: > Hello internals, > > The discussion about allowing never types as parameter types made me > think about what problem it is truly trying to solve, > which is using the same type as parameters and return values between > multiple methods of a common interface. > > This is normally solved with generics, but as shown generics are hard > TM [1] and a lot of the complexity resides in allowing the generic type > to be set at *use* site of the class with generic parameters. > However, this does not apply to template/generic types on interfaces, > as the bound type is determined by the implementing class at compile > time. > > As such, a few weeks ago, I created a PoC [2] where one can declare a > template/associated type T on interfaces that require an implementing > class uses the same type when specifying T. > > The syntax in the PoC is currently: > ``` > interface I { > type T : int|string; > public function foo(T $param): T; > } > class CS implements I { > public function foo(string $param): string { > return $param . '!'; > } > } > ``` > > I.e. the associated type is indicated by a "type" keyword, optionally > followed by a colon `:` and a type constraint. > The type corresponding to the associated type is currently "guessed" by > the first usage in a concrete class. > > Having talked with Arnaud off-list, it seems that using the "usual" > generic syntax of (assuming our parser can cope with it): > ``` > interface I { > public function foo(T $param): T; > } > class CS implements I { > public function foo(string $param): string { > return $param . '!'; > } > } > ``` > is possible and would not conflict with any future proposal for > generics. > > Importantly, this feature would NOT support doing the followings: > - `$o instanceof I` > - Using `I` in type declarations > > Meaning you do not get static type information, similarly to the > `never` as parameter type, > but one is guaranteed that there is no funky type variance and > incompatibilities between different methods (or parameter and return > types of the same method) in a concrete implementation of an interface. > > Effectively, static analysis tools must either assume T is `mixed`, or > hook it into their generic types feature. > Which is what the current status is for various interfaces (e.g. > ArrayAccess). > > I am intending on getting this feature ready for 8.5, and the reason I > bring it up to the list now, without a proper RFC, is to explain my > reasoning for why I am voting against the never parameter type RFC. [3] > > For any questions, feel free to reply, but please do remember that this > is still a bit in flux. > > > Best regards, > > Gina P. Banyard > > [1] https://thephp.foundation/blog/2024/08/19/state-of-generics-and-collections/ > [2] https://github.com/php/php-src/pull/18260 > [3] https://wiki.php.net/rfc/never-parameters-v2 If we make this work, I believe this solves the use cases for `never` far better. As I noted off-list (just putting it on record), if we're confident that the generic-style syntax won't interfere with future work on generics, I'd prefer that style. If it would cause issues, I'd favor the separate `type` syntax or something similar, but with an explicit rather than implicit declaration of what T should be. This would also effectively replace the custom syntax that Derick and I were considering for typed Collections a while back. A more generic solution like this would be superior either way. --Larry Garfield