Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110975 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 62644 invoked from network); 13 Jul 2020 16:41:35 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Jul 2020 16:41:35 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3713B180540 for ; Mon, 13 Jul 2020 08:33:46 -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,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 lavender.maple.relay.mailchannels.net (lavender.maple.relay.mailchannels.net [23.83.214.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 13 Jul 2020 08:33:44 -0700 (PDT) X-Sender-Id: dreamhost|x-authsender|josh@joshbruce.dev Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id D2B5A12039A; Mon, 13 Jul 2020 15:33:42 +0000 (UTC) Received: from pdx1-sub0-mail-a1.g.dreamhost.com (100-96-12-24.trex.outbound.svc.cluster.local [100.96.12.24]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id E591312151A; Mon, 13 Jul 2020 15:33:41 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|josh@joshbruce.dev Received: from pdx1-sub0-mail-a1.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384) by 0.0.0.0:2500 (trex/5.18.8); Mon, 13 Jul 2020 15:33:42 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|josh@joshbruce.dev X-MailChannels-Auth-Id: dreamhost X-Irritate-Keen: 528fcbeb698ba4ca_1594654422374_1221414151 X-MC-Loop-Signature: 1594654422373:2261030714 X-MC-Ingress-Time: 1594654422373 Received: from pdx1-sub0-mail-a1.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a1.g.dreamhost.com (Postfix) with ESMTP id 9D6DAB341F; Mon, 13 Jul 2020 08:33:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=joshbruce.dev; h=from :message-id:content-type:mime-version:subject:date:in-reply-to :cc:to:references; s=joshbruce.dev; bh=+ywXybbZJI1kPcdOjndQYq6u1 /0=; b=TV/Rkm5N0aOeXJ8zyYoDoLxfG7MfISgYrl6c94ZtXUd+fYI95HdTAxz6n pb3R3gN/9qCVJw5U5BPlM4tX8o+Qp72xq+GpgyR0PK317VMfa6NajUw40JsbgIjW 7dRzbyKaDBMv9lA+x2KvrrTPBUMoYtzd8/5uTZHAq53+gduq0c= Received: from joshs-mbp.lan (21.140.29.136.in-addr.arpa [136.29.140.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: josh@joshbruce.dev) by pdx1-sub0-mail-a1.g.dreamhost.com (Postfix) with ESMTPSA id EF023B3423; Mon, 13 Jul 2020 08:33:40 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a1 Message-ID: <820338EF-AD7C-4AD2-9305-8F814FA163D7@joshbruce.dev> Content-Type: multipart/alternative; boundary="Apple-Mail=_C4E040D2-3D3E-4F79-9283-7455998CAC5E" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.80.23.2.2\)) Date: Mon, 13 Jul 2020 10:33:39 -0500 In-Reply-To: Cc: php internals To: Nikita Popov References: <7238EFA9-8BE4-48E2-B6C6-6C3E14CFA208@joshbruce.dev> X-Mailer: Apple Mail (2.3608.80.23.2.2) X-VR-OUT-STATUS: OK X-VR-OUT-SCORE: -51 X-VR-OUT-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgeduiedrvdekgdelvdcutefuodetggdotefrodftvfcurfhrohhfihhlvgemucggtfgfnhhsuhgsshgtrhhisggvpdfftffgtefojffquffvnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenogfuuhhsphgvtghtffhomhgrihhnucdlgeelmdenucfjughrpefhkfgtggfuffgjvfhfofesrgdtmherhhdtjeenucfhrhhomheplfhoshhhuceurhhutggvuceojhhoshhhsehjohhshhgsrhhutggvrdguvghvqeenucggtffrrghtthgvrhhnpeeigeefveeiudevkeelffevvdduudevtdekvdejtdegtddugfejvdeukeevvedvgeenucffohhmrghinhepsghithdrlhihnecukfhppedufeeirddvledrudegtddrvddunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmohguvgepshhmthhppdhhvghlohepjhhoshhhshdqmhgsphdrlhgrnhdpihhnvghtpedufeeirddvledrudegtddrvddupdhrvghtuhhrnhdqphgrthhhpeflohhshhcuuehruhgtvgcuoehjohhshhesjhhoshhhsghruhgtvgdruggvvheqpdhmrghilhhfrhhomhepjhhoshhhsehjohhshhgsrhhutggvrdguvghvpdhnrhgtphhtthhopehinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprdhnvght Subject: Re: [PHP-DEV] [CONCEPT][DISCUSSION] Allow objects to declare emptiness and by extension truthiness From: josh@joshbruce.dev (Josh Bruce) --Apple-Mail=_C4E040D2-3D3E-4F79-9283-7455998CAC5E Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 >=20 > Latest version as we zero in on what this is really about: = https://bit.ly/php-0002 = > >=20 > I have a really hard time understanding what is actually being = proposed here. You mention the introduction of the Emptiable interface, = but not how it influences empty() or (bool) casts. There's some examples = in "Usage" that are possibly intended to illustrate this -- I suspect = that a "return true" vs "return false" mixup has made them impossible to = understand though. >=20 > Some thoughts on the general domain: >=20 > 1. I don't like the idea of decoupling empty() and (bool). empty($x) = is currently the same as !$x (modulo notices), and changing that will = make PHP's already messy implicit type conversion matrix even messier. >=20 Thanks for the feedback! Not all incorporated in the latest, due to = timing: https://bit.ly/php-0002 Initially this started as a desire to be able to declare an object = casting to bool (__toBool or something). After a couple rounds the = relationship between empty() and bool surfaced. The question I=E2=80=99m = volleying there is are they different concepts (a non-answer compared to = a lie) and does it matter given the established norm. (Sounds like = you=E2=80=99re in the established norm camp - I tend to lean that way as = well.) > 2. Coupling boolean casts to an "emptiness" concept seems somewhat = dubious to me. That works for container structures, but what about = something like new Bigint(0)? It would be quite odd to declare an = integer "empty" to make it evaluate to false. This seems counter to the first bullet point. They already are coupled. = The result of empty() tends to be the opposite of casting bool - unless = there=E2=80=99s an example I=E2=80=99m unaware of. >=20 > 3. I'm generally not a fan of overloadable bool casts. We have exactly = one object with an overloaded bool cast in PHP, and that's = SimpleXMLElement. As far as I know, this has only caused issues. A big = reason for that is that using "if ($foo->bar)" is a common idiom for = checking "if ($foo->bar !=3D=3D null)" -- one that works very reliably, = with the exception of SimpleXMLElement. I wasn=E2=80=99t aware of this. Thank you! I will take a look and see = what problems people are running into and how it=E2=80=99s implemented - = at least from a developer perspective. > 4. I think if ($container->isEmpty()) is much clearer than if = ($container) -- why would we want people to use the latter instead? Fair question. This made me raise the question in the draft and I=E2=80=99= ll raise it here=E2=80=A6under what circumstances would PHP return null = from new. $instance =3D new MyClass(); If ($instance->isEmpty()) {} $inner =3D $instance->innerObject(); If ($innnerObject->isEmpty()) {} If I implement the innerObject() method to return null - and it does - = then that call fails, because it=E2=80=99s null, which brings up your = earlier point, I think. If ($innerObject) { // not null If ($innerObject->isEmpty()) { // two-step process with null-check } } // with the language feature If ($innerObject) { // I know it can give and receive messages, and is not empty } else { // I know it can give and receive messages, but is empty } I remember hearing what sounds like hyperbole but it said something like = 40% of code written is guarding against null. I can totally see that = being real so didn=E2=80=99t look much further into it. If we can = guarantee that an object is that type of object - just happens to be = empty, maybe we reduce or remove the need null-checks?? Cheers, Josh --Apple-Mail=_C4E040D2-3D3E-4F79-9283-7455998CAC5E--