Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121532 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 15963 invoked from network); 30 Oct 2023 15:48:08 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Oct 2023 15:48:08 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 784CC180507 for ; Mon, 30 Oct 2023 08:48:08 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-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,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS29838 64.147.123.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wout3-smtp.messagingengine.com (wout3-smtp.messagingengine.com [64.147.123.19]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 30 Oct 2023 08:48:08 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 63DA532003CE for ; Mon, 30 Oct 2023 11:48:07 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Mon, 30 Oct 2023 11:48:07 -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:sender:subject :subject:to:to; s=fm3; t=1698680886; x=1698767286; bh=PnJq3FWqK7 nnMBCeXMbz3g4vP9E4BUkwMb1GNqPEK1U=; b=Tw6JbiE+KaM5iQZeNgGi1n63yA UayjLpqp+ALnwwLKTmxZWuk+tvzdQTcSJjvR3tHNVUF5laZ8PSIl/jMBWj3GBSKS KteoUfsucYs3F8InaJmQaYlO0KAHknfYqk2s7E1fLZqSg+mtPVFyvLKU7iSDmugu Ljr/xlTAeYHPjxjO/aomlhYs6zJmyCS8o1tDZWxyQR3uXhgyT33JTWTqzbKf9CQE zU8ACyGPCQRNGwgQpJ5+tJ6GkBjinLWMd34F2qJu9t0P3eKxquxZHrKQiIbWEo4K YXw7d9Jajm9lhP1Ke4Ai+ZNc3ywRIvSdZhyAvRsEGLcR31+4VF/IAGe0aJ+A== 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:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1698680886; x= 1698767286; bh=PnJq3FWqK7nnMBCeXMbz3g4vP9E4BUkwMb1GNqPEK1U=; b=j xyyhR3PS85tbJoDWeC22oHrJhm0+s3m4oCFddbRjwwRt5JG5XdRNuIEIrA0NcTFO J+i4tdm3vxOrqwMqWgnLzCg/gM5oMp7qn9S/p5/FlY+uV+0jJrArxkcJwDqCLXwt BVwkOtKD5+6e2RmlY/dKi7X4XoNyyObyRHaFoDudMTtUS0Lf5rvw7k+dkU9RgEjE 0vJwQF5afGxcbl2d2y436xckACNIgPDoxYdMp0ra3fX3jpL50BkoOJoYonx/YsL2 19PTeDr+6OHUk2OUELMPJmw5kjPFyWq8DpYqWu4r7RRE5Jt2XPD2dK7dHZtGNqAm XsLItkLNqOKNlB5/XGb6A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedruddttddgjeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgfgsehtqhertderreejnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepffffffejffdugfegvedviedttedvgfejffefffej leefjeetveehgefhhfdvgfelnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 9DA8D1700089; Mon, 30 Oct 2023 11:48:06 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-1048-g9229b632c5-fm-20231019.001-g9229b632 MIME-Version: 1.0 Message-ID: In-Reply-To: References: Date: Mon, 30 Oct 2023 15:47:44 +0000 To: "php internals" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Discussion - Anti-null coercion From: larry@garfieldtech.com ("Larry Garfield") On Mon, Oct 30, 2023, at 11:17 AM, Ilija Tovilo wrote: > Hi Robert > > On Sun, Oct 29, 2023 at 7:31=E2=80=AFPM Robert Landers wrote: >> >> Hello Internals, >> >> We currently have a null coercion operator: ??, but we lack an >> anti-null coercion operator. >> ... >> fn() =3D> >> ($_SERVER['HTTP_X_MY_HEADER'] ?? null) >> ? md5($_SERVER['HTTP_X_MY_HEADER']) >> : null; >> ... >> This is rather tedious when you have to do it, so, I'd like to discuss >> adding a new "anti-null coercion" operator: ?! >> >> This would collapse the previous verbose code into: >> >> fn() =3D> >> $_SERVER['HTTP_X_MY_HEADER'] >> ?! md5($_SERVER['HTTP_X_MY_HEADER']; > > This does not seem significantly less verbose to me. The main > motivation for ?? was that it avoids repeating the expression over > something like ?:. I would see a stronger argument for this feature if > it offered the same benefit. E.g. > > $_SERVER['HTTP_X_MY_HEADER'] ?! md5($$) > >> It would have a lower precedence than ?? so that the above line would >> read from left to right without requiring parenthesis/brackets. The >> operator would only return the right-hand side if the left-hand side >> exists (aka, not null), otherwise, it would return null. > > I think it should have a higher precedence. > > $_SERVER['HTTP_X_MY_HEADER'] ?! md5($$) ?? 'abc' > =3D=3D> > ($_SERVER['HTTP_X_MY_HEADER'] ?! md5($$)) ?? 'abc' > > Otherwise the result is NULL if the header is missing, given that the > coalesce operator is never executed. > > That said, while I've certainly encountered this situation, it's > nothing a temporary variable can't fix. I don't personally believe > there's a strong need for such an operator. > > Ilija Another point to note is that for objects, we already have ?->, which ef= fectively serves this purpose. It is sugar for: is_null($o) ? null : $o->whatever() So any new operator would only be relevant for free-standing variables (= which is automatically a code smell) or arrays, and I'm not convinced th= e latter is a large enough use case in a decently written code base. (Y= ou really should know what your data is, and the best way to do that is = map it into a proper object, at which point ?-> comes into play.) --Larry Garfield