Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126898 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 304001A00BC for ; Fri, 21 Mar 2025 15:52:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1742572173; bh=f3AU7OLhXNhlPKnXm3XN0KiqvK8rcvz37CWDxSfNvi8=; h=Date:From:To:In-Reply-To:References:Subject:From; b=OwJONAkVaXrIBttyHMve6+/KIQckwY6BSRvOUgrA7y/utI7tA5BPbrk3U6mINW0io APUd4GBA1/t2/8gp60PRdLk0ToR90I9bP12UaAJiZ4Z3icmWKQYx5iH8koYQZ6d8pg iOCe9337w0batQE9Q4IK9zljdnvR8H+ay0TasbX25YkCWkEh4oaPo5rbAbNZTS+VqM UfLfXBExdOpfTHMZ+Mx0aMrwzXcytUTTR+pRvYj8VJcrHv7HlDbFPmjf1tNF/eTjvW Mp70StZvWeWPz/5ESPfn0CK2JDstS1MlsayczTUxHDRzss0yYmmZ9Rd93TPTrEkKfU WXXs6sj+JVjzQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B5E8418005B for ; Fri, 21 Mar 2025 15:49:31 +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: No X-Envelope-From: Received: from fhigh-b7-smtp.messagingengine.com (fhigh-b7-smtp.messagingengine.com [202.12.124.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 ; Fri, 21 Mar 2025 15:49:31 +0000 (UTC) Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfhigh.stl.internal (Postfix) with ESMTP id 1F2F52540201 for ; Fri, 21 Mar 2025 11:52:01 -0400 (EDT) Received: from phl-imap-06 ([10.202.2.83]) by phl-compute-04.internal (MEProxy); Fri, 21 Mar 2025 11:52:01 -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=fm1; t=1742572320; x=1742658720; bh=3Q+spLxHzmlvnV28IdnUO qGOALaZoISDOP8TxaIrDww=; b=jsPcczRSyUrlywcE2jUEFvLqotITeqVPnoyjb k24YYjfS0qVKH3bnekdgneD3Ej50rpyhyxjXKyIotMinH0/Qlp/wAyJs+VIp/A8r BbGqxatcgLSpQ258hipNgIm50hHOysyTd4B2+O/+ntd+uoVOOsXGVt1EKuJ4uKgO 1mK+vjQiV2YTjR2RqPRhbQtSr75xw1Uykp3j2p+YJ7NcUVZLFLSTKYiPCYLSGkyA p7YYqJBh6wL/+MBnLJ4nk3Z4efhrxZgiCNqJr9cHbD0DLnqiiA7Ie9DiPk5Reef8 aca2axb+VBCc3+TJ3eSGeU19owicsxklIrMLlVmntSC1CwlIw== 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=fm1; t=1742572320; x=1742658720; bh=3 Q+spLxHzmlvnV28IdnUOqGOALaZoISDOP8TxaIrDww=; b=M2n129+4pR7C3sD1x 8S7FTzzOgePAQV9LTYoR46LXpDXjtGVESltmP8ViN6J+BJHNWpTGH1NojzuQJ6Be h6ImuOqheiLTNFzWUJVYXiG5WKYSsPA9jXwhzNj8RJNRWyA5e4401YpB5gA606ts W91UbaZUEFznZXogYX9K9yAXcoxb84Vbg+Van6mpvJVf4azPvmyTrySnyQwjtbWF hQUAzSugHKdsIDF1rZ/f0axcmJS/lRzJlEf7bMCm3IvZaSRaVa4zV99BMqSNmTIS HyA1Vl/Dd4HN+TMrYC0e89uoECVs46u/8mIi3fcWCZ5ZegA5JHGiZkBoJMK0h5Oo T7Kfw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduhedugeelucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpih gvnhhtshculddquddttddmnecujfgurhepofggfffhvffkjghfufgtgfesthhqredtredt jeenucfhrhhomhepfdfnrghrrhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrh hfihgvlhguthgvtghhrdgtohhmqeenucggtffrrghtthgvrhhnpeffieeivdfhvdeguddt tdegteeiueegvefhteehfeeffeetudeitdehtdegjeeuieenucevlhhushhtvghrufhiii gvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlught vggthhdrtghomhdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphhouhhtpdhrtg hpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id B04B129C0076; Fri, 21 Mar 2025 11:52:00 -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: T499b434fb43e9cfc Date: Fri, 21 Mar 2025 10:51:20 -0500 To: "php internals" Message-ID: <8cb36524-1f3d-49d6-9d7d-687bd3897a97@app.fastmail.com> In-Reply-To: References: <3e35cd25-c851-4ecc-8a1b-102dadb226e5@app.fastmail.com> Subject: Re: [PHP-DEV] [RFC] [Discussion] Never parameters Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") On Thu, Mar 20, 2025, at 6:02 PM, Daniel Scherzer wrote: > On Thu, Mar 20, 2025 at 4:00=E2=80=AFPM Larry Garfield wrote: >> I have a use case for this in Serde, so would be in favor. >>=20 >> We should not block this kind of improvement on the hope of generics.= Worst case, we have this plus generics so you have options, how terrib= le. >>=20 > > Would you mind sharing details of your Serde use case? It seems that=20 > the BackedEnum example might not have been the best (since it is for=20 > static methods) and so perhaps a userland case where this would be use= d=20 > would help. > > --Daniel Simplified example to show the thing we care about: I have an interface Formatter, like: interface Formatter { public function serializeInitialize(ClassSettings $classDef, Field $= rootField): mixed; public function serializeInt(mixed $runningValue, Field $field, ?int= $next): mixed; public function serializeFloat(mixed $runningValue, Field $field, ?f= loat $next): mixed; // And other methods for other types } The $runningValue is of a type known concretely to a given implementatio= n, but not at the interface level. It's returned from serializeIntializ= e(), and then passed along to every method, recursively, as it writes ou= t an object. So for instance, the JSON formatter looks like this: class JsonSerializer implements Formatter { public function serializeInitialize(ClassSettings $classDef, Field = $rootField): array { return ['root' =3D> []]; } /** * @param array $runningValue * @return array */ public function serializeInt(mixed $runningValue, Field $field, ?int= $next): array { $runningValue[$field->serializedName] =3D $next; return $runningValue; } } Because JsonFormatter works by building up an array and passing it to js= on_encode(), eventually. So $runningValue is guaranteed to always be an= array. I can narrow the return value to an array, but not the paramete= r. The JsonStreamFormatter, however, has a stream object that it passes aro= und (which wraps a file handle internally): class JsonStreamFormatter implements Formatter { public function serializeInitialize(ClassSettings $classDef, Field $= rootField): FormatterStream { return FormatterStream::new(fopen('php://temp/', 'wb')); } /** * @param FormatterStream $runningValue */ public function serializeInt(mixed $runningValue, Field $field, ?int= $next): FormatterStream { $runningValue->write((string)$next); return $runningValue; } } Again, I can narrow the return value but not the param. To be clear, generics would absolutely be better in this case. I'm just= not holding my breath. Associated Types would probably work in this case, too, since it's alway= s relevant when creating a new concrete object, not when parameterizing = a common object. If we got that, I'd probably use that instead. Changing the interface to use `never` instead of `mixed` would have the = weakest guarantees of the three, since it doesn't force me to use the *s= ame* widened type on serializeInt(), serializeFloat(), serializeString()= , etc., even though it would always be the same. But it would allow me = to communicate more type information than I can now. How compelling this use case is, I leave as an exercise for the reader. --Larry Garfield