Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128953 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 lists.php.net (Postfix) with ESMTPS id 0F4A01A00BC for ; Fri, 24 Oct 2025 13:58:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1761314321; bh=Kq01nnkC/ccDeF2qmkBzBT4AslEz6R+szS2sZhcgXFo=; h=Date:From:To:Subject:In-Reply-To:References:From; b=f7eggwJIWvhRoET4qhw4x+MtAphyGHsVu73O4yOVGizm4Df/baQES18LHAeYliPRc aH9vnAzpa6bIEh/siFkTBX3PcWraVwjpop4cqE+/sPAL/aDt1TVFwQq3isV3feh5oN 15WyBLVKPrQT2Qcmedog82y1fQJH6y1Si5yetJ8w2U34Zp6lfOkWbyu0RDGh56IGQ/ s36XkZFvPvVv/e5oSntFHaywjsaqnC5YXbe/FAhpq64uReT4Du5bdlcJiLkErcQ3x+ eW7+St3kAX6YYwPREv9sMdXedl1V3YmZTMnfETrT+oMTs7nfPIxyy7EoHl1l3l73if rLrPDgYcZq4Nw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B92DF18007E for ; Fri, 24 Oct 2025 13:58:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) 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_PASS autolearn=no autolearn_force=no version=4.0.1 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 ; Fri, 24 Oct 2025 13:58:40 +0000 (UTC) Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfhigh.stl.internal (Postfix) with ESMTP id 9A9E97A01A0 for ; Fri, 24 Oct 2025 09:58:34 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-01.internal (MEProxy); Fri, 24 Oct 2025 09:58:34 -0400 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=fm2; t=1761314314; x=1761400714; bh=Kq01nnkC/ccDeF2qmkBzBT4AslEz6R+szS2sZhcgXFo=; b= EsdJa3wSy7VwShaPnFAnA3ep+hv/BxHEVPSwu7bRPLOmEHgZzmhHX+Ti7XfPpdoR MLQmta6vZ/0F7sFhgznniBP5gH1ieW+HThsf0kcVTIw0F8TJG6mjMYw24a3UGf1r HCvVvKdioiuRikc/0Jks/+fsPs5Idi+UNblWpmfZrcrgFi6hQVvo62cKNDrQT2D2 6WfaSSTOcxPkjzd79tHogrkJujDGWK6EwLUWGYWOjHEzA5G0OoA6SuSYvRD3mbIU q37AlPf8bfO0r6f8AA7Z1V1Giyr4DAjRZa4iGrYthnfEax1kI6tqJBhf+w+L17ud 4XHRUoctvrBWUzSZgJT2Aw== 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=1761314314; x=1761400714; bh=K q01nnkC/ccDeF2qmkBzBT4AslEz6R+szS2sZhcgXFo=; b=zARyMa14kqSvXZviF LcTFFFt8Q8wY4BXdy09NMHf6hKDJ7Ece49BIzxhsCwYi6qh4kKXUWwV05kzmQqXB IbJBLVROdG8c+m9yqClBxDh2Tu2nw4LtiDvtogrYa5c/uePLjA/QKhJmVPygFWsD X89d0BXqwt52ldmBnIEbyglmZ4U3nCDhObpc/6JG7qQ7w+R6vWv2DErdpF4EhKAB TP5WfoAIfhf2qdgCSswwwonJ2+SBgcUTLv7VCxRBf8qM8vkFouA1KcKZVz9uwU9l Ho3T94EFymvwobiIqWGMES0XjcUOzUf16+V8Wnn5Kd3FziP28Ms6z0j56peg7bbG 9TEBQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggddugeelhedvucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhepfffhvffufggjfhfkgggtgfesthhqmhdttderjeenucfhrhhomhepfdftohifrghn ucfvohhmmhhinhhsucglkffoufhorfgnfdcuoehimhhsohhprdhphhhpsehrfigvtgdrtg hordhukheqnecuggftrfgrthhtvghrnhepledvueehtdehudduleefhffgjefhffdvleej fefgueeigfeigfdvhefhfeeuvddvnecuffhomhgrihhnpehphhhprdhnvghtnecuvehluh hsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepihhmshhophdrphhh phesrhifvggtrdgtohdruhhkpdhnsggprhgtphhtthhopedupdhmohguvgepshhmthhpoh huthdprhgtphhtthhopehinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Fri, 24 Oct 2025 09:58:33 -0400 (EDT) Date: Fri, 24 Oct 2025 14:58:30 +0100 To: PHP internals list Subject: Re: [PHP-DEV] [RFC] Nullable and non-nullable cast operators User-Agent: K-9 Mail for Android In-Reply-To: References: Message-ID: Precedence: list list-help: list-unsubscribe: list-post: List-Id: 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 24 October 2025 08:07:02 BST, Alexandre Daubois wrote: >Hi everyone, > >Nicolas Grekas and I would like to propose this new RFC to the >discussion=2E This proposes to introduce two new cast operators to the >language: `(?type)` and `(!type)`=2E > >Please find the details here: >https://wiki=2Ephp=2Enet/rfc/nullable-not-nullable-cast-operator Hi both, and thanks for the RFC=2E PHP definitely needs a better range of cast operators, and I've been think= ing for a while about what that should look like=2E My main concern with th= is proposal is that it picks a few specific options and uses syntax in a wa= y that's not easy to extend=2E What I mean by that is that there are, roughly, three things that a user m= ight want to specify: 1) What *output types* are desired? 2) What *input values* are considered castable? 3) What should happen for *invalid* values?=20 For (1), our current casts allow base scalar types + array=2E I can defini= tely see value in allowing nullable types there, but also other complex typ= es, e=2Eg=2E (string|Widget)$foo could be equivalent to $foo instanceof Wid= get ? $foo : (string)$foo For (2) and (3), you could say our current casts accept anything, but you = could argue that they replace invalid values with a fixed "empty" value fro= m the target type=2E More importantly, there are a few different things you might *want* to hap= pen: - throw an exception, because you expect the cast to succeed, and want to = abort if it doesn't - fill with null, or with some default value from the target type, e=2Eg= =2E because you're *sanitising* untrusted data and want to proceed with the= best you can get - detect that it *would* fail, e=2Eg=2E because you're *validating* data= =2E Throwing exceptions is generally a poor choice for validation or sanitisat= ion, because you have to either make the user fix one mistake at a time, or= add a separate try-catch around every check=2E The current RFC adds support for nullable types (1), but only if you *also= * want a different set of validation rules (2) *and* a different behaviour = if those rules aren't met (3)=2E That's a *useful* combination, but it's no= t the *only* useful combination=2E Using the (?type) syntax for that combin= ation makes it hard to add other combinations in future=2E Rowan Tommins [IMSoP]