Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114652 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 46546 invoked from network); 27 May 2021 22:41:27 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 27 May 2021 22:41:27 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B49141804CC for ; Thu, 27 May 2021 15:53:18 -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=-1.1 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) (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 ; Thu, 27 May 2021 15:53:18 -0700 (PDT) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 832745C00CE for ; Thu, 27 May 2021 18:53:17 -0400 (EDT) Received: from imap8 ([10.202.2.58]) by compute3.internal (MEProxy); Thu, 27 May 2021 18:53:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=Wp/QcKpLsdigwC+qQ2hqnoZhk3XFT8prtadlmVFkl jU=; b=UQMpwPr2OgucQi7t7qI1ubDWr1ghSv4QqnqGYhxn8TF1HET/dpdS9ZIG+ j1F+XqSdu/YOOKwZBU6w1UbxWfctbK4lqua1vIqubGVPrp0Bta4CldS9rj+8Sm+G Ui2uteAvSXHKU7mcj78HXlwTDlX4DtsIXoV19+gM8PwDsICEv6Sk/bCcOYsiL84O FpH2uRwoCNallfrs9K97egpfp5HFxM4DqtL0coC9nXaSOisXjLrQ+l+ZtqA7hDsI 0x66Q8Zy38Rl11/PnyBfMT5uMBGwYVmRO7eyvaE17c+Hlu+87VPPbV+i/tjMCK5d 3AOQ0IybS7Vr5shmBOGA7eoLNvy3w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvdekiedgudefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgfgsehtqhertderreejnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepffffffejffdugfegvedviedttedvgfejffefffej leefjeetveehgefhhfdvgfelnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 38F023A0310; Thu, 27 May 2021 18:53:17 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-468-gdb53729b73-fm-20210517.001-gdb53729b Mime-Version: 1.0 Message-ID: In-Reply-To: <0936e748-c0af-4b4e-7bb3-808d399811c7@gmail.com> References: <80fb5a8e-d770-49fd-45d5-ea6f4b004513@gmail.com> <2874cf7c-e55d-4100-bb01-af11a5ab6e87@www.fastmail.com> <93bee614-8bca-7e35-94a6-518beafac8ba@processus.org> <0936e748-c0af-4b4e-7bb3-808d399811c7@gmail.com> Date: Thu, 27 May 2021 17:52:57 -0500 To: "php internals" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Consensus Gathering: is_initialized From: larry@garfieldtech.com ("Larry Garfield") On Thu, May 27, 2021, at 8:23 AM, Rowan Tommins wrote: > The combination of union types and enums actually gives a much more=20= > expressive way of representing "valid value or explicit null or specia= l=20 > default". To use the ORM lazy-loading example: >=20 >=20 > enum ORMState { > =C2=A0=C2=A0=C2=A0 case Unloaded; > } >=20 > class Example { > =C2=A0=C2=A0=C2=A0 private int|ORMState|null $foo =3D ORMState::Unloa= ded; >=20 > =C2=A0=C2=A0=C2=A0 public function getFoo(): ?int { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if ( $this->foo =3D=3D=3D = ORMState::Unloaded ) { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 $t= his->foo =3D $this->loadFromDatabase('foo'); > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return $this->foo; > =C2=A0=C2=A0=C2=A0 } >=20 > =C2=A0=C2=A0=C2=A0 private function loadFromDatabase($fieldName): ?in= t { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 echo "Fetching '$fieldName= ' from database...\n"; > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return 42; > =C2=A0=C2=A0=C2=A0 } > } >=20 > // Create object; note there's no constructor, but the property has a=20= > default so is never uninitialized > $obj =3D new Example; > // On first call to method, the default value is detected and the=20 > property lazy-loaded > var_dump($obj->getFoo()); > // Subsequent accesses return the loaded value directly, at the cost o= f=20 > a single strict comparison, no magic function needed > var_dump($obj->getFoo()); I had not thought of this use of Enums, but I love it. It's not as eleg= ant as a tagged enum for a monad, but it gets you 80% of the way there. = Especially if you combine it with match(). enum Result { case None; } function find_user(int $id): User|Result { ... } $user =3D match($u =3D find_user($id)) { Result::None =3D> some error handling, default: $u, }; --Larry Garfield