Re: [RFC[ Property accessor hooks, take 2
| From: | Larry Garfield | Date: | Mon, 26 Feb 2024 20:24:24 +0000 |
| Subject: | Re: [RFC[ Property accessor hooks, take 2 | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-122508@lists.php.net to get a copy of this message | ||
On Mon, Feb 26, 2024, at 7:39 PM, Rowan Tommins [IMSoP] wrote:
> On 26/02/2024 19:02, Frederik Bosch wrote:
>>
>> That's how it always has been, no? So in your example, short code
>> abbreviated form would not work. One has to write a block.
>>
>> public string$fullName {
>> set=> [$this->first, $this->last] = explode
>> <http://www.php.net/explode>(' ', \ucfirst
>> <http://www.php.net/ucfirst>($value)); // error, $fullName is
>> a string, returning array
>> }
>>
>> public string$fullName {
>> set{
>> [$this->first, $this->last] = explode
>> <http://www.php.net/explode>(' ', \ucfirst <u4s
>> ³ΕRóÙ-¦TÙÕhttp://www.php.net/ucfirst>($value)); // no error, not returning
>> }
>> }
>
>
> I think the intention is that both the block and the arrow syntax would
> have any return value ignored, as happens with constructors, for
> example. Note that in PHP, there is actually no such thing as "a
> function not returning a value", even a "void" function actually returns
> null; so if the return value was treated as meaningful, your second
> example would give an error "cannot assign null to property of type string".
This correct. Given a function test():
$ret = test('Larry Garfield');
There's no way to tell if $ret is a possibly-null value we should do something with, or null by
side-effect. The RFC right now takes the stance of "it's null by side effect, always, so
we never do anything with the return so it's consistent."
> However, as noted in a previous message, I agree that the short form
> meaning "the value returned is saved to the backing field" is both more
> expected and more useful.
You're the first person to comment on it, but I'm glad you agree. :-) I like it, but
Ilija is still unsure about it.
> The "yield" idea is ... interesting. I think personally I find it a bit
> too magic, and too cryptic to be more readable than an explicit
> assignment. Opinions may vary, though.
>
> Regards,
Mixing in syntax only used for generators here seems like it's asking for trouble. It
wouldn't actually be a coroutine, so using coroutine like syntax would just be confusing.
It's confusing to me whether this implies the hook becomes a generator or not, which means
it's likely to confuse a lot of other people.
--Larry Garfield