Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113236 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 5978 invoked from network); 23 Feb 2021 18:21:54 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Feb 2021 18:21:54 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4971F180505 for ; Tue, 23 Feb 2021 10:10:27 -0800 (PST) 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,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 23 Feb 2021 10:10:26 -0800 (PST) Received: by mail-lf1-f49.google.com with SMTP id u4so12735035lfs.0 for ; Tue, 23 Feb 2021 10:10:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=7Jspg481XMXwDUwO0MmDvR076jaRPuo8/qF8+d6i2oA=; b=sLzzncg1tMGqziOvG5XMwJDP5iE49s9Kh7wwPjM69UV2h1mOuUFerF+wJGnQgB+UpR aJhhX42IUTd5n0W+NfSZOvrNa8sfGHGijLXWrEXejIoNXiUxtyDdtFbABVBIECjfPRBi MB8VY70oFXDtboYR6GGdVVYJVSHOh3RT/legNdcGsiQZz9YafNvex7W/soS2ge+odbvQ 5vFZ3hzt89hrW2IPBC9Z+GjP3j8AgJxlmcimSCe8nCVJwOla6j4ofXcqH0TJncC3+M6E xHyQJWOh5qXyipMZarIRo+7t9scgcl7NfB1onNe5c5NYO02mo9cEcocT5ElAMiicPL6o p/yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=7Jspg481XMXwDUwO0MmDvR076jaRPuo8/qF8+d6i2oA=; b=A5VpfuDhTzNGubraiSNKfLqTFbevfF32NwVoL7c3klQzCIPQ7krP77sUWU0IHoENIp 9Sr2XbhFhykHt5CrAnza1cr4fBo80f+PDTHMcoK6p1ApiuZNPGrLGBK1g0e/mX5LHZxH Y7K7vrfsONUXUDtfH4KYvGcFZjOGBfwaWME/djNe1u34UgrhtsSdm8j/xRt2VlAuZEj2 BK4sRhTtJFeq0vJWQZP9U7EnKQmAkNSneM+DBK1U61Pr9FhlEItJRPj+WXZ2sTIe+YRG uCMyT0LxzhK4Vz+dimsPCSlA2nN6OLMCTSlYygN95QmBhgNzpORDAPBwmPtfzinRwaye /uVQ== X-Gm-Message-State: AOAM531kHszI0un3TtOmXd18WLAfQFqCgQVMFKouru5yR1Cgwc4bJlva ipyZewQzIAj9ONGfRpg9BCJ7tU6mOiEe7brjxG1gh47TQnzL X-Google-Smtp-Source: ABdhPJxdETvt5AuPhBhJi37yED254hlJPHqO04rUJ0N8UaN/gkVKI2AXf8+9V/tZd3AUQIvYGUMNokAmZYf6OXc7H5I= X-Received: by 2002:a19:7e93:: with SMTP id z141mr11012153lfc.486.1614103825135; Tue, 23 Feb 2021 10:10:25 -0800 (PST) MIME-Version: 1.0 References: <2664e2ef-b965-407c-90fc-77480846a3ad@www.fastmail.com> In-Reply-To: <2664e2ef-b965-407c-90fc-77480846a3ad@www.fastmail.com> Date: Tue, 23 Feb 2021 19:10:15 +0100 Message-ID: To: php internals Content-Type: multipart/alternative; boundary="00000000000020bde305bc04d514" Subject: Re: [PHP-DEV] Inline conditional that returns null if falsy From: guilliam.xavier@gmail.com (Guilliam Xavier) --00000000000020bde305bc04d514 Content-Type: text/plain; charset="UTF-8" On Tue, Feb 23, 2021 at 6:28 PM Larry Garfield wrote: > On Tue, Feb 23, 2021, at 10:49 AM, Albert Casademont wrote: > > Another example is when a scalar input needs to be either left null or > > converted to a Value Object: > > > > $color = $data['color'] ? new Color($data['color']) : null; > > > > This would become; > > > > $color = $data['color'] ? new Color($data['color']); //the ": null" part > is > > implicit > > > > It's actually kinda the inverse of the "?:" operator. > > > > As Guilliam said, Twig already implements such behaviour, it's quite > handy > > when dealing with nullable variables, > > 1) Please don't top post. > > 2) > > The advantage of ?: over long-ternary is that the part it lets you omit is > of variable size, and is often verbose (nested array elements). For completeness: it's also important when implying side effects, e.g. `foo() ?: bar()` calls foo() only once, while `foo() ? foo() : bar()` may twice. That's not the case here, as the omitted portion is a fixed length short > constant value (": null"). So the value of the abbreviation is much less. > Indeed, and (as I said too) I'm not even sure whether it improves readability or instead worsens it. (By the way, I mentioned Twig because David's HTML example (Smarty maybe?) reminded me of it, but that's a "template engine"; I actually don't know a "general purpose" programming language with a C-like ternary operator that allows omitting the ": null" part.) > > I am also not a fan of null being commonly used, as it is a dangerous > value. More often I would not want null if the color were missing but some > default color value, which would be represented by something other than > null anyway. You seem to join Marco here ;) And "falsy" is, as we've been reminded numerous times, a dangerous and > tricky thing. (I just had several PRs against one of my OSS libraries > because it relied on falsy, which had all sorts of incorrect failure > conditions.) > I would just add that it's also true for "?:" (i.e. not specific to the suggestion at hand, although the lack of a non-controversial example so far may be a hint) > > I'd be a -1 here. > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > -- Guilliam Xavier --00000000000020bde305bc04d514--