Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121514 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27418 invoked from network); 29 Oct 2023 20:24:09 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Oct 2023 20:24:09 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 62869180087 for ; Sun, 29 Oct 2023 13:24: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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-oo1-f54.google.com (mail-oo1-f54.google.com [209.85.161.54]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 29 Oct 2023 13:24:07 -0700 (PDT) Received: by mail-oo1-f54.google.com with SMTP id 006d021491bc7-581e5a9413bso2269418eaf.1 for ; Sun, 29 Oct 2023 13:24:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698611047; x=1699215847; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=wrOk9ZeoC+d8ntnwOHQeCz+SeQ/3lFmgJTPq6sC/tVw=; b=EovQQa2QwjkhHHkSClkOt/9HGatpfmemUuwrF6skYFiCWzMirJWd/qQwTFgvDwYGaM ByWd/P6OHrwdSQzPZ67RXNbNlKL+HL7fq1vwhWZoDX/H1sWj9O5SbWfoUdovEE8NepVt Kb8RE6ALsvQNYr3EGrlktkUybw+IvaLMuM0R/we06hITice9llhWZWOYGASRBeGt7ok4 9oCmASyGMc3TUmkopwUxPRGXWqU0OB8waXbGYageDZIqpT+l3iDnqhhRdJqCWzPUg9BB fpqlrwm4FkVxwBS8Ng4lr5VoR4nm6sGLcBwcMBauBAR95CFg9NvaM6R3GrdgAMRZiT76 kRWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698611047; x=1699215847; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wrOk9ZeoC+d8ntnwOHQeCz+SeQ/3lFmgJTPq6sC/tVw=; b=n7vdKk9dxit+jaE6W0J3X92mSqXy37H4wje0Tq4Ni5Asmk5K6gCrpGwLRwqfR9ViDs kgSB4ZLya9z60wgx2A+1m/6byVxzF0yKmQYHDxumntXLB0sQik6V9zKQwuP+XW4lXBWN /PszEprtvN/7bddkkuxnqZJt1M9rBgHIIg2QAUGYsWXBU6gxCdQRCtI5ewwelN2Cxz/F LbAm2j4iOQiortfI6xS4aq01zio7CNQ5+FCzL5TR1Pw3sY+54vK08qAdNqoTik3/7WYo UGuF1KxA/VXUhF6D9ejlXiszTMuOp/zan5YSSJMm+94NHUF++93971GMNwJJbHP23w7u hdIw== X-Gm-Message-State: AOJu0Yzx6hOJbx0tnWAYIo6jk6HQ15LU/PbsBwypuC2MIbozssHxUT2S iolWvoKDAaQjS4WoXntQMr2g6kKJno5/uoi4pDHn2OP61Ek5Lw== X-Google-Smtp-Source: AGHT+IEfbh7OKx/hZtWNcoditF75Tn6WTc96XD3YHnKdEHsAb6CY7WUI9iUvPM6m268w5V6PACp0YZjGuZSms6xiV9g= X-Received: by 2002:a05:6830:22db:b0:6c0:abdd:a875 with SMTP id q27-20020a05683022db00b006c0abdda875mr9401498otc.18.1698611046989; Sun, 29 Oct 2023 13:24:06 -0700 (PDT) MIME-Version: 1.0 References: <0CDEE8D1-7905-44B4-9C00-0251B94222C4@shymko.net> In-Reply-To: <0CDEE8D1-7905-44B4-9C00-0251B94222C4@shymko.net> Date: Sun, 29 Oct 2023 21:23:54 +0100 Message-ID: To: Sergii Shymko Cc: internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Discussion - Anti-null coercion From: landers.robert@gmail.com (Robert Landers) On Sun, Oct 29, 2023 at 8:53=E2=80=AFPM Sergii Shymko w= rote: > > > > On Oct 29, 2023, at 1:31 PM, Robert Landers wr= ote: > > Hello Internals, > > We currently have a null coercion operator: ??, but we lack an > anti-null coercion operator. > > For example, if I wanted to operate on a header, if-and-only-if it > exists, I'd have to write something like this one-liner: > > fn() =3D> > ($_SERVER['HTTP_X_MY_HEADER'] ?? null) > ? md5($_SERVER['HTTP_X_MY_HEADER']) > : null; > > Or something like this: > > function() { > if(!empty($_SERVER['HTTP_X_MY_HEADER']) { > return md5($_SERVER['HTTP_X_MY_HEADER']); > } > > return 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']; > > When it is null, it will stay null, thus the above is the same as: > > fn() =3D> > $_SERVER['HTTP_X_MY_HEADER'] > ?! md5($_SERVER['HTTP_X_MY_HEADER'] > ?? null; > > 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'm not particularly attached to the ?! syntax (since it does, in > fact, look very similar to ?:), so perhaps focusing on the merits of > the idea first, then bikeshedding the syntax later would be a good > approach? > > Thoughts? > > Robert Landers > Software Engineer > Utrecht NL > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > > Hi Robert, > > Why don=E2=80=99t you combine the two examples and use a regular ternary = operator along with isset/empty()? > > fn() =3D> > !empty($_SERVER[=E2=80=98HTTP_X_MY_HEADER=E2=80=99]) > ? md5($_SERVER[=E2=80=98HTTP_X_MY_HEADER=E2=80=99]) > : null; > > It=E2=80=99s only ever so slightly longer than the proposed ?! operator, = but way more powerful. You explicitly control the evaluation expression alo= ng with the default value. > > Regards, > Sergii Hey Sergii, > Why don=E2=80=99t you combine the two examples and use a regular ternary = operator along with isset/empty()? Because falsy is different than null/non-existent. The closest equivalent is "array_key_exists" (instead of empty/isset) but this proposal isn't just applicable to arrays. I run into this pattern of "if not null, then do something with the value" pretty often and it is tedious to type it out. Robert Landers Software Engineer Utrecht NL