Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113165 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 78494 invoked from network); 13 Feb 2021 16:36:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Feb 2021 16:36:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B44B61804CF for ; Sat, 13 Feb 2021 08:22:16 -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=-0.7 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ot1-f43.google.com (mail-ot1-f43.google.com [209.85.210.43]) (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 ; Sat, 13 Feb 2021 08:22:16 -0800 (PST) Received: by mail-ot1-f43.google.com with SMTP id i20so2202889otl.7 for ; Sat, 13 Feb 2021 08:22:16 -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 :cc; bh=cLMfZkL1+PcceAj4UuBzagQY0c/LgLDKtLlyTlNOANQ=; b=XEU9Q3n4COTNIvGe2Jfb3wjtNV1cpqaEZlbSLXtOHoFc9W4TRp7AbcCOtysIMznsTm dGoDN66xhv8oMgrp16aB86a6tLwdwKEXiBeUaXYL2xOrDUefK/xatNJb82NJ82179Lpj Bj31VjdTb1hXKgzDzsiPumracfwfzk6FxVprzSEXoYlvcoUE0o11D5l6DKbGizcDxzMK TmgotJlbuRSm+ZbNO9RpO6iDeYx07WlvQt0CVK8wRytcPGQ2oaXiGtWSQLe/w5ADojFf q2+Vs/IsJnluRVg1+sVNInPEh3+3iqrsy1Oe4ozn3fmW6FiH2LqUgr+JcuFPh3ICcAkL xNiQ== 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:cc; bh=cLMfZkL1+PcceAj4UuBzagQY0c/LgLDKtLlyTlNOANQ=; b=grlxUxWbqNHBS1GAdgdDGU6Wth4dP/50PEALInj+wvrBtnu9tivxOVmcxwEdwNJaAS YcxB9wSM05exhi7Krl7iI1sHGWW3GGTe29BC6vGv/JxqigCEfBtwmHwB5srmmBsURImm /GdGe2y4lkoxjPn9hvTPRUaDAmCmWv4tyE6mhsW+wDYz7fAowb+4iV1b4GtZpk0ZzWfa EaGwIOw00tJAXT9Q2Ft9r+DdxpiwAv1EPYH1mu8d79u25DfTMXhF0OHop07//+tSSFWB yPuauOr7DTSPSMHLaoGtbXid/reWAH8SNmokr5hTWYlQQOpVwq0+opMoYez+Wz+FbjXh pqLQ== X-Gm-Message-State: AOAM530Z7cFWLQS5QhFRf1euoqr2v+Tq883tzcGuQb2VOtuJQtN79W9o ++d0bCBef7NI5tL9jMFplT9hIMZ1pJpD5yXtssw= X-Google-Smtp-Source: ABdhPJy04/s3iFvkKnWbL4vBb8qncAhanVGFU9VnSQ/odZ2OJSWgYvdXi2Q+iejRDfhH/FELAT7TKAzaMe4EEA8fu1I= X-Received: by 2002:a05:6830:1609:: with SMTP id g9mr5768118otr.209.1613233334979; Sat, 13 Feb 2021 08:22:14 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 13 Feb 2021 13:22:02 -0300 Message-ID: To: Marco Pivetta Cc: PHP Internals Content-Type: multipart/alternative; boundary="000000000000df111805bb3a2701" Subject: Re: [PHP-DEV] Inline conditional that returns null if falsy From: david.proweb@gmail.com (David Rodrigues) --000000000000df111805bb3a2701 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Em s=C3=A1b., 13 de fev. de 2021 =C3=A0s 00:11, Marco Pivetta escreveu: > Hey David, > > Even upfront, this looks like a quite bad idea: either you have an empty > collection, or you have a collection with elements in it, but `null` is a > very different concept that isn't homogeneous with the resulting type. > > I've updated multiple projects in the past where `array|null` was used as > return type for a collection-alike endpoint, removing `null` and replacin= g > it with an empty iterable instead. > This was just one of the examples I have, but I still think it is valid. Creating an empty collection costs resources that could be avoided in situations where null could be used (as is most of my real use cases). But there are other situations where we can use different situations to use this type of resource. One of the examples I have is when I need to include a class in an HTML element conditionally. There are probably better ways, but this is a quick one for simple situations:
"online" }}">...
vs.
...
It is much easier to read, and I believe that the cost for language would be minimal, and perhaps even more optimized than the use of ?:. Atenciosamente, David Rodrigues Em s=C3=A1b., 13 de fev. de 2021 =C3=A0s 00:11, Marco Pivetta escreveu: > Hey David, > > On Fri, Feb 12, 2021, 20:24 David Rodrigues > wrote: > >> Hello! >> >> It is just a suggestion to be discussed. >> >> A lot of places on my projects I have codes like: >> >> $companies =3D $user->companies->count() >> ? new Collection($user->companies) >> : null; >> > > Even upfront, this looks like a quite bad idea: either you have an empty > collection, or you have a collection with elements in it, but `null` is a > very different concept that isn't homogeneous with the resulting type. > > I've updated multiple projects in the past where `array|null` was used as > return type for a collection-alike endpoint, removing `null` and replacin= g > it with an empty iterable instead. > >> --000000000000df111805bb3a2701--