Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122512 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 847FF1AD8F6 for ; Tue, 27 Feb 2024 09:49:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1709027388; bh=NJ/OefSlZ/180mXGpFNhnJUwDMSb2ktZT+A4wsRQpSQ=; h=Date:From:To:Subject:In-Reply-To:References:From; b=TJBZXPS/LSifHUN2prL+XWfDvBg1T0RyzmX6grgnZdI9CIE00RxGrPLNEtsVRGL3/ XC9HCoXZ2H2bHPfa6Teo69V67RSZ+1bGNzmDdhjXuYQJtMNIURzJ6+piTjrNmRxffz 1hkcpZO5zM6F/aMiJbzaIR0vCYVe+xgeO3VopI/uXH5VLvcOEntPpEoRPV+zbXCFhz Oed/u55ndXiWtly+wf869sRTmBMJF4voxbWHLip9H3hub6HkR8XJ0az3T3bnBmCoQC iGZ7sAeqE2Tdi3DobTpti9WHKbU7Pw7UUYKVzLWLacnopLgemabV2Iq/zAVP3iTobm 8pvFGsLQ1RrrA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D3D92180059 for ; Tue, 27 Feb 2024 09:49:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from fhigh4-smtp.messagingengine.com (fhigh4-smtp.messagingengine.com [103.168.172.155]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 27 Feb 2024 01:49:46 -0800 (PST) Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfhigh.nyi.internal (Postfix) with ESMTP id 6A1B411400D3; Tue, 27 Feb 2024 04:49:37 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Tue, 27 Feb 2024 04:49:37 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1709027377; x=1709113777; bh=NJ/OefSlZ/180mXGpFNhnJUwDMSb2ktZT+A4wsRQpSQ=; b= helq89/iao3gf16rtXcklwkdnTSpqcvS84sPs3MDgSWlBYHTtg/HVzM4XofJStaz 5hOC35+j4iyyUsHaN41DgyVtlW29IzINjaL3pNj4NMPMDo46QVGpE9qxXYD6Tmpb mAtCf/fvQDH76tB1aLUJHPfUWBBIH2VhyDxwKCSLSgvr49+69QDNJ0d1R57tE3cI 29ZdWNdLBWoJZu9QeqjvCaovFzKVWvCJn4nDM2nzRaiRXtbitLA4TMoa+n0ik+sg KvBzjkZs2zYSZa0YdkEChS18jsA8cO/VKyMo1tk01F93AEwQEah86jM4NjwkDlUQ DFqnC2dzaQtyM4DjK98e3A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1709027377; x= 1709113777; bh=NJ/OefSlZ/180mXGpFNhnJUwDMSb2ktZT+A4wsRQpSQ=; b=G mBk5/atE+Rj5geM4ofvdIUCi5D33xmxvv+pHIhcona5aDzaV8fCeVnrQIu6r6Kza RC1qLG9bol6FciwIh/gOjYFklvOb+LwqmqRhVGISchP5S29ycXOqmruYdRsKvzC+ aj3AIy0EDhsr0LbyfZbGiJk1uxFokKtSEaFdj5AJcqOj7TxujC1wa5nBQzFAFZwL Z+uFK+jCmpr5AXE5ZWhrn87UbaLsOse0mBzVXM3ZqI5jTDeHuFCZBqy7xrsVpR8B FbHm8wSD3SSSP2oLxNvvjN0ekbA/w8ecodrSwM2dwi9XNP+301y6RGVRfWvWuCmv A7BVI11ytseXvJJx40gyQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrgeeggddtlecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvufgfjghfkfggtgfgsegrjehmredtreejnecuhfhrohhmpedftfhofigr nhcuvfhomhhmihhnshculgfkoffuohfrngdfuceoihhmshhophdrphhhphesrhifvggtrd gtohdruhhkqeenucggtffrrghtthgvrhhnpeeuffffvefffeehgfelgedtvefgvdfgheet jedthedvveegjeejudduieeuheefhfenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpehimhhsohhprdhphhhpsehrfigvtgdrtghordhukh X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 27 Feb 2024 04:49:36 -0500 (EST) Date: Tue, 27 Feb 2024 09:49:35 +0000 To: internals@lists.php.net, Frederik Bosch Subject: Re: [PHP-DEV] [RFC[ Property accessor hooks, take 2 User-Agent: K-9 Mail for Android In-Reply-To: <963f5cc5-cdb1-4384-b519-5cb15640654e@genkgo.nl> References: <790b5b4e-f51b-4050-a12a-5fa903d0568f@app.fastmail.com> <52C6F501-8E23-42D7-8541-88A22AD79375@koalephant.com> <36e90d8d-d275-4ce9-9dd9-1e2422c6d3a9@app.fastmail.com> <2fdf1933-b51c-40cc-8d02-31899b96c71c@genkgo.nl> <95e93cb9-3ab0-4cf3-8ec5-83e74c9dd607@genkgo.nl> <876aff9f-3eae-4d2d-8e3f-30dfbbeed49c@rwec.co.uk> <963f5cc5-cdb1-4384-b519-5cb15640654e@genkgo.nl> Message-ID: <58B82A81-8A89-4F17-B982-7FC36404032E@rwec.co.uk> Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Content-Type: multipart/alternative; boundary=----THPRZVTWNDWO5NK9O5RCSVN5SO3GES Content-Transfer-Encoding: 7bit From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") ------THPRZVTWNDWO5NK9O5RCSVN5SO3GES Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 26 February 2024 23:11:16 GMT, Frederik Bosch wr= ote: >And what happens in the following situation, how are multiple get calls w= orking together? > >public string $fullName { >=C2=A0=C2=A0=C2=A0 get =3D> $this->first =2E ' ' =2E $this->last; // is t= his accessing the backed value, or is it accessing via get >=C2=A0=C2=A0=C2=A0 set($value) =3D> $this->fullName =3D $value; >} > >public string $first { >=C2=A0=C2=A0=C2=A0 get =3D> explode(' ', $this->fullName)[0], // is this = accessing the backed value, or is it accessing via get >=C2=A0=C2=A0=C2=A0 set($value) =3D> $value; >} I don't think it's *that* confusing - the rule is not "hooks vs methods", = it's "special access inside the property's own hook"=2E But as I say, I'm c= oming around to the idea that using a different name for that "backing fiel= d" / "raw value" might be sensible=2E >> What would happen if a setter contained both "return 42;" and "return;"= ? The latter is explicitly allowed in "void" functions, but is also allowed= in a non-void function as meaning "return null;" >return 42; // returns (int)42 >return; // early return, void, same as no return >return null; // returns null I'm not sure if you misunderstood my question, or just the context of why = I asked it=2E I'm talking about a hook like this: set($value) { if ($value) { return 42; } else { return; } } Currently, the only definition of "void" in the language is that a void fu= nction must not contain an explicit return value=2E We could turn that chec= k around, and deduce that a certain hook is void=2E This hook would not pas= s that check, so we would compile it to have an assignment, and the false c= ase would assign null to the property=2E To avoid that, we would need some = additional analysis to prove that in all possible paths, a return statement= with a value is reached=2E The alternative would be to run the code, and somehow observe that it "ret= urned void"=2E But "void" isn't a value we can represent at run-time; we wo= uld need to set the return value to some special value just for this specif= ic case=2E We would have to turn that on just for hook bodies, as returning= it from normal functions would be a huge BC break, and also not very usefu= l - with union types, there would be plenty of better options for a functio= n to indicate a return value that needs special handling=2E >$generator =3D setCall($class, 'first', $value); >foreach ($generator as $value) { >=C2=A0=C2=A0 writeProperty($class, 'first', $value); >} >if ($generator->hasReturn()) { >writeProperty($class, 'first', $generator->getReturn()); >} That's already an order of magnitude more complicated than "the return val= ue is used on the right-hand side of an assignment", and it's missing at le= ast one case: set($value) { return $value; } will not compile to a generato= r, so needs to skip and assign the value directly=2E By "magic", what I meant was "hidden logic underneath that makes it work"= =2E Assign-by-return has a small amount of magic - you can express it in ha= lf a line of code; assign-by-yield has much more magic - a whole bunch of l= oops and conditionals to operate your coroutine=2E > The yield is much more intuitive than magic fields I think we'll just have to differ in opinion on that one=2E Maybe you're j= ust more used to working with coroutines than I am=2E Note that yield also doesn't solve how to read the current backing value i= n a get hook (or a set hook that wants to compare before and after), so we = still need some way to refer to it=2E Regards, Rowan Tommins [IMSoP] ------THPRZVTWNDWO5NK9O5RCSVN5SO3GES Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On 26 February 2024 23:11:16 G= MT, Frederik Bosch <f=2Ebosch@genkgo=2Enl> wrote:

>And what= happens in the following situation, how are multiple get calls working tog= ether?
>
>public string $fullName {
>=C2=A0=C2=A0=C2=A0 g= et =3D> $this->first =2E ' ' =2E $this->last; // is this accessing= the backed value, or is it accessing via get
>=C2=A0=C2=A0=C2=A0 set= ($value) =3D> $this->fullName =3D $value;
>}
>
>pub= lic string $first {
>=C2=A0=C2=A0=C2=A0 get =3D> explode(' ', $thi= s->fullName)[0], // is this accessing the backed value, or is it accessi= ng via get
>=C2=A0=C2=A0=C2=A0 set($value) =3D> $value;
>}
I don't think it's *that* confusing - the rule is not "hooks vs metho= ds", it's "special access inside the property's own hook"=2E But as I say, = I'm coming around to the idea that using a different name for that "backing= field" / "raw value" might be sensible=2E


>> What would h= appen if a setter contained both "return 42;" and "return;"? The latter is = explicitly allowed in "void" functions, but is also allowed in a non-void f= unction as meaning "return null;"
>return 42; // returns (int)42
&= gt;return; // early return, void, same as no return
>return null; // = returns null

I'm not sure if you misunderstood my question, or just = the context of why I asked it=2E I'm talking about a hook like this:
set($value) { if ($value) { return 42; } else { return; } }

Current= ly, the only definition of "void" in the language is that a void function m= ust not contain an explicit return value=2E We could turn that check around= , and deduce that a certain hook is void=2E This hook would not pass that c= heck, so we would compile it to have an assignment, and the false case woul= d assign null to the property=2E To avoid that, we would need some addition= al analysis to prove that in all possible paths, a return statement with a = value is reached=2E

The alternative would be to run the code, and so= mehow observe that it "returned void"=2E But "void" isn't a value we can re= present at run-time; we would need to set the return value to some special = value just for this specific case=2E We would have to turn that on just for= hook bodies, as returning it from normal functions would be a huge BC brea= k, and also not very useful - with union types, there would be plenty of be= tter options for a function to indicate a return value that needs special h= andling=2E



>$generator =3D setCall($class, 'first', $valu= e);
>foreach ($generator as $value) {
>=C2=A0=C2=A0 writeProper= ty($class, 'first', $value);
>}
>if ($generator->hasReturn()= ) {
>writeProperty($class, 'first', $generator->getReturn());
&= gt;}


That's already an order of magnitude more complicated than = "the return value is used on the right-hand side of an assignment", and it'= s missing at least one case: set($value) { return $value; } will not compil= e to a generator, so needs to skip and assign the value directly=2E

= By "magic", what I meant was "hidden logic underneath that makes it work"= =2E Assign-by-return has a small amount of magic - you can express it in ha= lf a line of code; assign-by-yield has much more magic - a whole bunch of l= oops and conditionals to operate your coroutine=2E



> The = yield is much more intuitive than magic fields

I think we'll just ha= ve to differ in opinion on that one=2E Maybe you're just more used to worki= ng with coroutines than I am=2E

Note that yield also doesn't solve h= ow to read the current backing value in a get hook (or a set hook that want= s to compare before and after), so we still need some way to refer to it=2E=

Regards,
Rowan Tommins
[IMSoP]
------THPRZVTWNDWO5NK9O5RCSVN5SO3GES--