Re: [RFC] Default expression
| From: | John Coggeshall | Date: | Sun, 25 Aug 2024 20:32:04 +0000 |
| Subject: | Re: [RFC] Default expression | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125237@lists.php.net to get a copy of this message | ||
> public function suspend(float|int|null $delay = null) {
> parent::suspend((int)(($delay ?? 0) * 1000) ?: default);
> }
> }
>
> new MySuspension()->suspend(2.2345); // int(2234)
> Not only have I demonstrated the need to use multiplication or division
> to change the scale, but also the need to cast.
>
I appreciate what you're saying here.
I've been struggling a little bit to really nail my language here on what I think should and
shouldn't be allowed. Essentially I'm trying to say (and I think others are too) is this:
The engine should not allow the use of default in an expression that doesn't ultimately
evaluate to default *.
In the above example the left - hand of the ?: operator doesn't use default , so it's
evaluation is whatever it's evaluation is. The right-hand of the ?: operator DOES use default ,
and thus it must evaluate ultimately to default or that would be an error. Another example:
parent::foo((default >= 10) ? default : 10)
Would be permitted because the left-hand uses default , but the evaluation if the conditional where
default was used for the true case true is default . Likewise the right-hand is just 10 and
irrelevant
This would not be permitted
parent::foo((default >= 10) ? (default + 1) : 10)
Because now the ultimate evaluation of default is default + 1 -- not default
*The exception to the rule I've described above would be IFF the expression default is in only
uses specific allowed operators like a subset of the bitwise operators.
I very much appreciate that what is being described here is a significant effort to achieve,
I'm not even sure it's reasonably possible.. but I just can't get behind the idea
that (default)->foobar() is a valid expression in this context or a good idea for the language.
The use of this proposed default keyword must have guardrails IMO. I think my definition above is a
pretty reasonable attempt at capturing where I think the line is here and hopefully that helps guide
this discussion.
John