Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126918 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 8197E1A00BC for ; Sun, 23 Mar 2025 18:37:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1742754902; bh=/9s2gDlo6XSZWNlnSN4aRA/HRtCKPG9XnKDxJcV6YLc=; h=Date:Subject:To:References:From:In-Reply-To:From; b=dtIKe45haYuFLnCCtPILMqsMXxA+fry/MO8nUD/ROKoEdMwCUl62uxCtEUeof9aQx 3VWtz5OTHLBsGQL75KcAUgGK5Jn1vo3YBoMghuM8v3zOzeD4vfKauiPLC+DJRBiTAX gh0esa35E9JroTXewmzG+yLHlkKmyKBU82KU55LeCM9nl/1x0QavjfWerw0WCX5eXc LS3H/eZkFY8cLxnU12K9SVYRK/6PXEZVK0qSgOpS/7ImF8Y1S6yExG/3Zy0CLVl7zp 33b5RviQ69zRBYLO6pDLLOmzUZQ6DpCeazbYtlm4wEeJ0Vv5ANq/qJ/c4slnK0/UeQ 569MMCqiqUcCQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 93AB7180053 for ; Sun, 23 Mar 2025 18:35:01 +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, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-a1-smtp.messagingengine.com (fhigh-a1-smtp.messagingengine.com [103.168.172.152]) (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 ; Sun, 23 Mar 2025 18:35:01 +0000 (UTC) Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfhigh.phl.internal (Postfix) with ESMTP id 9A33C114010D for ; Sun, 23 Mar 2025 14:37:30 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-11.internal (MEProxy); Sun, 23 Mar 2025 14:37:30 -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=fm1; t=1742755050; x=1742841450; bh=A2Aib3Q/NmY/JwO0r0PRDCRfP3Ezx81R9WUFpdRluDk=; b= a13pLg6aDa1jGlMbIkpbPQOlPwo1jiDbTxwOByM9aIC6Qr3LABp+JE4R1KrK1ZVk NxYN/iNRIpCQ3LlAoDk0coeRry0BBJXJaysvJwyv4uBLTaw6h1sln+3fu/PV1gHQ Zd6oeg3bqDeHGy10FjxYANI+G7k6tHU0IrIuiIRHX5EFP2X/sJYdoO9oxBcQSxab DG3MigUBDrha6vo7ERpeIKnlu/AAEksOs66qjSM5b98oQqq0E2ORFTehG3KlAdfG 62abYK37xlq/g5UCQspnRp9DwDAgeen9okN78aG6mZnjTWsG6pUu0lqh/Dda8jpM cUAPVG+6Nw4Isxo+9LKTEQ== 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=1742755050; x=1742841450; bh=A 2Aib3Q/NmY/JwO0r0PRDCRfP3Ezx81R9WUFpdRluDk=; b=N+QwXWQNSvtTZe4oi u2KB8mdpyhtMOpeNcgWLTLxREQ00Bqn1I5B1dMgnmZyehjyrh/Nr/+fxL4wh5hr4 a0DSGjvejN9BqO2EQPGG643oRXEXozVk+/+LvTD1/hM+ya/wVkjkO7Zh+RozQ7Ri I86R47Xt0LOocB0FoJOVGyd79LlspA9x+oX8oVdt2sBlNF9PaYlDVvZpqz8whg8y q2n3NZ9SIXPuSLrDxScc388uLMNzQ4iTKYT60nCaj7LVewA9ByUMJT/FykudiM83 uKZ7tdk9ijIypLg41tZ2fO/0vyZDm9rZm7fDeYLfUTjGn8kSxYZd/PdhAnbjxKBj gEd8Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduheejieduucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefkff ggfgfuvfhfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpedftfhofigrnhcuvfhomhhm ihhnshculgfkoffuohfrngdfuceoihhmshhophdrphhhphesrhifvggtrdgtohdruhhkqe enucggtffrrghtthgvrhhnpeffkeevudffuddvheejvdefkeelfedtudegfeehjeduheeg ieduffeggeegveefheenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpehimhhsohhprdhphhhpsehrfigvtgdrtghordhukhdpnhgspghrtghpthht ohepuddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepihhnthgvrhhnrghlsheslh hishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Sun, 23 Mar 2025 14:37:29 -0400 (EDT) Message-ID: <3c3ba603-5101-4084-8745-c34b5cd3cfd8@rwec.co.uk> Date: Sun, 23 Mar 2025 18:37:25 +0000 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] Feedback for nullc Idea To: internals@lists.php.net References: <010f0195c30f8ccb-c7fa839e-ec18-4613-9851-9d96b943dca2-000000@us-east-2.amazonses.com> Content-Language: en-GB In-Reply-To: <010f0195c30f8ccb-c7fa839e-ec18-4613-9851-9d96b943dca2-000000@us-east-2.amazonses.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On 23/03/2025 12:52, Robert Chapin wrote: > Hi PHP folks, > > I submitted a proposal on Monday that received no replies.  The idea > was about adding a functional construct nullc() that could act as an > inline null coalesce for a variable.  That task is currently not > possible in a nullc() user function, but instead requires an > expression like ($var ?? null) with mandatory extra parentheses. I'm sympathetic to the problem you're trying to solve - the precedence of ?? isn't always helpful - but I'm not sure I like the proposed solution, for 3 reasons. 1) The name "nullc" is over-shortened and cryptic. The "c" looks almost like an accident, and it doesn't actually do anything if given a null. 2) With only one argument, it isn't really "coalescing" anything, it's just suppressing errors. It's telling that all your examples with ?? put a value other than null on the right-hand side. If the default is not one of the normal values, the effective default depends on how you use it: if (nullc($test) === 'on') if (nullc($test) !== 'off') In my experience, a lot of people struggle to follow logic like this, and it can easily lead to subtle bugs. Compare the same logic with explicit defaults: if (nullc($test, 'off') === 'on') if (nullc($test, 'on') !== 'off') 3) The function-like syntax doesn't seem to gain us much; it has to be a language construct not a true function, so it won't be usable with things like array_map, and with two arguments, nullc($foo, $bar) and ($foo ?? $bar) look very similar. All of which makes me look to the COALESCE function from SQL: - The name is short but not cryptically abbreviated, and will be familiar to most PHP users. - The function is variadic - you can pass just one argument, but can also pass three, or ten. - The parameters are evaluated lazily, like they would be with an operator. I've seen and implemented many variations of this in userland, although obviously using `$arg !== null` rather than `isset($arg)`. There is a small risk that some people might have an existing function with different behaviour, e.g. a test on "emptiness" with `$arg != false`, and need to rename it when upgrading PHP. I'm still only lukewarm on including it, because it's mostly just reminding you to include a pair of parentheses: if (($_POST['input'] ?? null) === 'yes') echo 'success'; if (coalesce($_POST['input']) === 'yes') echo 'success'; if (coalesce($_POST['input'], null) === 'yes') echo 'success'; if (($_POST['input'] ?? $_GET['input'] ?? 'N/A') !== 'N/A') echo 'meaningful value provided'; if (coalesce($_POST['input'], $_GET['input'], 'N/A') !== 'N/A') echo 'meaningful value provided'; -- Rowan Tommins [IMSoP]