rfc:default_expression
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:default_expression [2024/08/28 22:35] – bilge | rfc:default_expression [2024/08/29 21:46] (current) – Added two seconary discussion concerns bilge | ||
---|---|---|---|
Line 103: | Line 103: | ||
<code php> | <code php> | ||
$f = fn ($v = 1, $default = 2) => $v + $default; | $f = fn ($v = 1, $default = 2) => $v + $default; | ||
- | var_dump($f(default: default + 1)); // int(4) | + | $f(default: default + 1); // int(4) |
</ | </ | ||
Line 138: | Line 138: | ||
Currently the only known failure case is lookup of trampoline functions, which can be created by calling < | Currently the only known failure case is lookup of trampoline functions, which can be created by calling < | ||
+ | |||
+ | ===== Discussion ===== | ||
+ | |||
+ | The greatest concern is the proposed grammar is too permissive and has drawbacks. Secondary concerns include evaluating < | ||
+ | |||
+ | ==== Limiting grammar ==== | ||
+ | |||
+ | The most common request is to constrain the allowed expression list. As already noted, some expressions don't make much sense because they probably don't have any practical application, | ||
+ | |||
+ | <code php> | ||
+ | // Expressions with default as output only. | ||
+ | F(1 ? default : 0) | ||
+ | F(1 ? 1 : default) | ||
+ | F(0 ?: default) | ||
+ | F(null ?? default) | ||
+ | F(match(1) { 1 => default }) | ||
+ | </ | ||
+ | |||
+ | Further, some even expressed concerns about allowing any expressions at all and would only be comfortable allowing < | ||
+ | |||
+ | Critics converged on a [[https:// | ||
+ | |||
+ | <code php> | ||
+ | class C { | ||
+ | public function F(int $V = 1) {} | ||
+ | } | ||
+ | |||
+ | class D extends C { | ||
+ | public function F(int|string $V = ' | ||
+ | } | ||
+ | |||
+ | function test(C $C) { | ||
+ | $C-> | ||
+ | } | ||
+ | |||
+ | test(new C); // OK. | ||
+ | test(new D); // Fatal error: Uncaught TypeError: Unsupported operand types: string + int. | ||
+ | </ | ||
+ | |||
+ | ==== Default as a dummy value ==== | ||
+ | |||
+ | Currently < | ||
+ | |||
+ | ==== Defaults as a contract ==== | ||
+ | |||
+ | Some have argued allowing < | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 156: | Line 202: | ||
</ | </ | ||
- | Regardless of how you vote above, we'd like to collect feedback on which limitations of this proposal would make it/still be acceptable for you, starting from the least significant to the most drastic | + | Regardless of how you vote above, we'd like to collect feedback on which limitations of this proposal would make it/still be acceptable for you, starting from the least significant to the most significant |
- | <doodle title=" | + | <doodle title=" |
* No union types | * No union types | ||
* Only conditional expressions | * Only conditional expressions | ||
Line 165: | Line 211: | ||
</ | </ | ||
- | Only the result of the primary vote has a clear path forward for default arguments to be included in PHP. If the primary vote fails, the secondary vote may inform whomsoever wishes to pursue a follow-up for limited | + | Only the result of the primary vote has a clear path forward for inclusion into the language. None of the proposed alternatives come with any feasibility guarantees. However, if the primary vote fails, the secondary vote may inform whomsoever wishes to pursue a follow-up for limited |
===== Appendix I: Further examples ===== | ===== Appendix I: Further examples ===== | ||
Line 259: | Line 305: | ||
// Match | // Match | ||
F(match(default) { default => default }) | F(match(default) { default => default }) | ||
+ | |||
+ | // Callable | ||
+ | F((default)-> | ||
// Parens | // Parens | ||
Line 276: | Line 325: | ||
F(print default) | F(print default) | ||
</ | </ | ||
- | |||
- | ===== Discussion ===== | ||
- | |||
- | The biggest concern is the proposed grammar is too permissive and has drawbacks. A secondary concern is default values are now part of an object' | ||
- | |||
- | ==== Limiting grammar ==== | ||
- | |||
- | The most common request is to constrain the allowed expression list. As already noted, some expressions don't make much sense because they probably don't have any practical application, | ||
- | |||
- | <code php> | ||
- | // Expressions with default as output only. | ||
- | F(1 ? default : 0) | ||
- | F(1 ? 1 : default) | ||
- | F(0 ?: default) | ||
- | F(null ?? default) | ||
- | F(match(1) { 1 => default }) | ||
- | </ | ||
- | |||
- | Further, some even expressed concerns about allowing any expressions at all and would only be comfortable allowing < | ||
- | |||
- | Critics converged on a [[https:// | ||
- | |||
- | <code php> | ||
- | class C { | ||
- | public function F(int $V = 1) {} | ||
- | } | ||
- | |||
- | class D extends C { | ||
- | public function F(int|string $V = ' | ||
- | } | ||
- | |||
- | function test(C $C) { | ||
- | $C-> | ||
- | } | ||
- | |||
- | test(new C); // OK. | ||
- | test(new D); // Fatal error: Uncaught TypeError: Unsupported operand types: string + int. | ||
- | </ | ||
- | |||
- | ==== Defaults as a contract ==== | ||
- | |||
- | |||
===== References ===== | ===== References ===== |
rfc/default_expression.1724884513.txt.gz · Last modified: 2024/08/28 22:35 by bilge