Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118971 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 13398 invoked from network); 4 Nov 2022 23:05:23 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Nov 2022 23:05:23 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E7039180044 for ; Fri, 4 Nov 2022 16:05:22 -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,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS36483 23.83.208.0/21 X-Spam-Virus: No X-Envelope-From: Received: from dormouse.elm.relay.mailchannels.net (dormouse.elm.relay.mailchannels.net [23.83.212.50]) (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 ; Fri, 4 Nov 2022 16:05:21 -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 5383A5C1905; Fri, 4 Nov 2022 23:05:20 +0000 (UTC) Received: from pdx1-sub0-mail-a314 (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id B9F035C195E; Fri, 4 Nov 2022 23:05:19 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1667603119; a=rsa-sha256; cv=none; b=cezYJwnSZZgQo+nEQU4dFyc7WEu22oCms7oNpFbhb8mrfcQ8nUD1zGi1eGwF3W7EuLl4bT 3zyziTd/eRIxv3eJtfcYotj1ianKDXgKOGJdT67x/HkOYxejPgfiOhcDGD3gHnGvVBDh5S ZU/MTzFFS3rPcmgLWDe0bh8ZnFQn2uwlR9Q2EdkU2pd8cPrbYV/G5HQQFyRFyqgxwEy/HC O/ivspNHB4OUY1izLAvla3BmocJqWYTLKdgG+NxNzuAL6v0WnO4PcxTn+RPQZCXGDvxd93 F025mcTnCCerSIoOIKJjHKm8t40yr66hH+UOu6/BM5iePvJ4DAUu5IfmIw+Ozw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1667603119; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bCUSd0L4V9YXZfNkkJe8P0mCjf/cD9GChyqxwjju9Rk=; b=ZYzx0O9akT3nFnD4dNESVEQ4dU5OdoWEIVE0PzoDHr6Qla7K5cDGqX17gmm7lz8F9CWSV1 +dzN8xJwkIJDzXMdt2J1h0RNqhwkJ9q0t7SlXT4AwvvPELGPsWV/+jnXyh4K4e7x+g4C75 HGmdq9Max0Wi+RDfFVT/WYUA0LtrlbrftrvojX/HHsQ7c63cTfeYT7GPFrO/2SdNClsy5V xQCvTGY6KMkoODzLH5k9WG7IhegD5z/3oE2llFLZIlUg5UtTnBFUBW3537iRoKb7+eEYxW Y50twR4Ahx7/npT4eKq/Zlb3+xneycxaJQietuGJgaHx6SFo+npBUC4mjLxv4A== ARC-Authentication-Results: i=1; rspamd-7f9bbcf788-k65ph; auth=pass smtp.auth=dreamhost smtp.mailfrom=josh@joshbruce.dev X-Sender-Id: dreamhost|x-authsender|josh@joshbruce.dev X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|josh@joshbruce.dev X-MailChannels-Auth-Id: dreamhost X-Tart-Skirt: 03081342227756cb_1667603120110_4017063796 X-MC-Loop-Signature: 1667603120110:1800615735 X-MC-Ingress-Time: 1667603120110 Received: from pdx1-sub0-mail-a314 (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.108.114.9 (trex/6.7.1); Fri, 04 Nov 2022 23:05:20 +0000 Received: from smtpclient.apple (111.sub-75-244-34.myvzw.com [75.244.34.111]) (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-a314 (Postfix) with ESMTPSA id 4N3x6k45MxzMB; Fri, 4 Nov 2022 16:05:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joshbruce.dev; s=dreamhost; t=1667603119; bh=bCUSd0L4V9YXZfNkkJe8P0mCjf/cD9GChyqxwjju9Rk=; h=Content-Type:Subject:From:Date:Cc:Content-Transfer-Encoding:To; b=FJK/MtaWcl5vWq/FciDKDI5NQinr2XCs8JJB+TJR16sSlIXdDu8/gYLhIpfIb2Qwb WkgYSYGQWto3EXJVZYVe0CURNNKbR4PiXBPLJQSS/Rzvf7boFpMZUtbEDV6/6WWsOO pxE7+JneLaGXf/wvWYCd36d/kIjadW5vgVmB8Md2Ushc9kPqFwB7p/PdbkdMDZMqsc 0j60TeRgw6mnReN2NIbv0xuD5G4UI1CqkXTPGIBo5xPcUVTYJ+IiJYINlCPdCfiXsr NH5hQLdmE7hDR02TDujU0h1PFjChsrt8AnS+CG08hzaxhl1yijKbICyEBDCCUa38z7 7LwxrYL/nuQrg== Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.200.110.1.12\)) In-Reply-To: Date: Fri, 4 Nov 2022 19:05:06 -0400 Cc: Claude Pache , Marco Pivetta , php internals Content-Transfer-Encoding: quoted-printable Message-ID: <3D76083B-6742-4026-824C-30458B110FEC@joshbruce.dev> References: <21C5073D-3F39-49DA-8686-E027AE780793@joshbruce.dev> <1bc7759a-63b8-4b96-bb69-97b50f851307@www.fastmail.com> <94DC0839-E43B-4F3A-9105-8F3CB9D7BBC1@joshbruce.dev> <9AE33306-ED89-4FE4-86A5-89B4EDC81545@gmail.com> <13A9249B-C3B2-46E3-AB50-D46FAA2A301E@joshbruce.dev> To: =?utf-8?Q?Micha=C5=82_Marcin_Brzuchalski?= X-Mailer: Apple Mail (2.3731.200.110.1.12) Subject: Re: [PHP-DEV] [RFC][Discussion] Objects can be declared falsifiable From: josh@joshbruce.dev (Josh Bruce) > While I understand the proposed feature is opt-int it introduces more = magic > that can be solved using more verbose and IMO cleaner solutions. Understood and appreciate the position. > This is somehow confusing, why is the $response storing object ref is = ok > while inclining the new object creation is not? Not quite following, apologies. Adding a different use case. Allow for fully-specified custom collections; array being the compound = type that can resolve to false/empty based on count. $array =3D []; (bool) $array; // false empty($array); // true if ($array) {=20 // handle not empty=20 }=20 // handle empty $array =3D [1, 2, 3]; (bool) $array; // true empty($array); // false if ($array) {=20 // handle not empty=20 }=20 // handle empty Current state of PHP re custom collections (exclamation marks - ! - = represent difference compared to native array): class MyCollection implements ArrayAccess, Iterator, Countable {} $collection =3D new MyCollection([]); (bool) $collection; // true - ! - because instance exists, regardless of = collection content empty($collection); // false - ! - same reason as previous if ($collection) {=20 // ! - handle empty and not empty=20 }=20 // ! - unreachable $collection =3D new MyCollection([1, 2, 3]); (bool) $collection; // true - because instance exists, regardless of = collection content empty($collection); // false - same as previous if ($collection) {=20 // ! - handle empty and not empty=20 }=20 // ! - unreachable With RFC: class MyCollection implements ArrayAccess, Iterator, Countable, = Falsifiable=20 { public function __toBool(): bool { return $this->count() > 0; } =20 } $collection =3D new MyCollection([]); (bool) $collection; // false - because collection count =3D=3D=3D 0 - = close to the previous comment re single value under inspection empty($colleciton); // true - same as previous if ($collection) {=20 // handle not empty=20 }=20 // handle empty $collection =3D new MyCollection([1, 2, 3]); (bool) $collection; // true - because collection count > 0 empty($colleciton); // false - same as previous if ($collection) {=20 // handle not empty=20 }=20 // handle empty Alternative approaches for custom collection use case:=20 1. Modify Countable (most likely, if using alternative) to include = empty() and bool() methods.=20 Might feel more palatable despite not strictly limiting scope to the = custom collection use case.=20 Any class with Countable, for example, would also be able to resolve to = false or empty using SPL functions - including use in IF. Allows for explicit (=E2=80=9Cverbose"), direct call of both methods by = user should they choose: $collection->empty() $collection->bool() Instead of $collection->__toBool() OR something like = $collection->__isEmpty() Known drawback to using Countable would be in a possible future where = there is a separation between 0, false, and empty when it comes to type = juggling in PHP.=20 Whereas using a Falsifiable interface leaves a simpler path of an = Emptiness interface; again, should the three be separated in the future. 2. Or, modify ArrayAccess to include the empty() and bool() methods. This would restrict the behavior more toward the custom collection = concept. However, would lean more toward Countable due to the equivalence in PHP = type juggling and Countable using an integer: 0 =3D=3D false =3D=3D = empty Otherwise, similar to alternative #1. 3. Or, modify Iterator to include the empty() and bool() methods (or = possibly leverage the valid() method that already exists?); otherwise, = similar to alternative #2. Cheers, Josh > On Nov 4, 2022, at 1:37 AM, Micha=C5=82 Marcin Brzuchalski = wrote: >=20 >>=20 >> if ($response->getStatusCode() > 199 and $response->getStatusCode() < = 300) >> { >> // do something with =E2=80=9Ctrue=E2=80=9D - which has a range of = 100 possibilities at >> a granular level, which we could respond to differently - possible to >> interact with $response >>=20 >> } >> // do something with =E2=80=9Cfalse=E2=80=9D - which has a range of = more than 100 >> possibilities at a granular level, which we could respond to = differently - >> possible to interact with $response >>=20 >> We might wrap response to create an isOk() method to move the = conditional >> logic somewhere within the object itself and make the call site more >> readable. >>=20 >> If ($response->isOk()) { >> // do something with =E2=80=9Ctrue=E2=80=9D - still able to interact = with $response >>=20 >> } >> // do something with =E2=80=9Cfalse=E2=80=9D - still able to interact = with $response >>=20 >=20 > This looks way much cleaner and is easy to read and understand. > While I understand the proposed feature is opt-int it introduces more = magic > that can be solved using more verbose and IMO cleaner solutions. >=20 >=20 >> With the RFC: >>=20 >> if (new MyType($config)) { >> // do something with =E2=80=9Ctrue=E2=80=9D - can=E2=80=99t use = MyType because not assigned >>=20 >> } >> // reachable because possible to resolve to false - if implements >> Falsifiable and __toBool can resolve to false - can=E2=80=99t use = MyType because >> not assigned >>=20 >> if ($response =3D new MyType($config)) { >> // do something with =E2=80=9Ctrue=E2=80=9D - with the option of = using $response >>=20 >> } >> // reachable - can=E2=80=99t use MyType because may not be assigned >>=20 >> $response =3D new MyType($config); >> If ($response) { >> // do something with =E2=80=9Ctrue=E2=80=9D - with the option of = using $response >>=20 >> } >> // do something with =E2=80=9Cfalse=E2=80=9D - with the option of = using $response >>=20 >=20 > This is somehow confusing, why is the $response storing object ref is = ok > while inclining the new object creation is not? > This requires more attention while reading and debugging. >=20 > Cheers, > Micha=C5=82 Marcin Brzuchalski