# [RFC] Default expression

- Thread: https://externals.io/message/125183
- Original archive: https://news-web.php.net/php.internals/125183
- Started by Bilge on 2024-08-24T16:49:26+00:00
- Messages: 101
- RFC: Default expression (Withdrawn) — https://externals.io/rfc/default_expression.md
- Page 1 of 5: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/125183.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #125183 — Bilge — 2024-08-24T16:49:26+00:00

URL: https://externals.io/message/125183#125183

Hi gang,

New RFC just dropped: https://wiki.php.net/rfc/default_expression. I 
think some of you might enjoy this one. Hit me with any feedback.

This one already comes complete with working implementation that I've 
been cooking for a little while. Considering I don't know C or PHP 
internals, one might think implementing this feature would be 
prohibitively difficult, but considering the amount of help and guidance 
I received from Ilija, Bob and others, it would be truer to say it would 
have been more difficult to fail! Huge thanks to them.

Cheers,
Bilge

---

## #125185 — Matthew Weier O'Phinney — 2024-08-24T17:01:55+00:00

URL: https://externals.io/message/125183#125185 · In reply to: #125183

> Hi gang,
>
> New RFC just dropped: https://wiki.php.net/rfc/default_expression. I
> think some of you might enjoy this one. Hit me with any feedback.
>

This is a feature I've wanted for a very long time! The RFC is very
straight forward, and the appendix does a great job of enumerating the
possible expressions.

Nice work all around!


> This one already comes complete with working implementation that I've
> been cooking for a little while. Considering I don't know C or PHP
> internals, one might think implementing this feature would be
> prohibitively difficult, but considering the amount of help and guidance
> I received from Ilija, Bob and others, it would be truer to say it would
> have been more difficult to fail! Huge thanks to them.
>
> Cheers,
> Bilge

---

## #125188 — Bilge — 2024-08-24T19:05:17+00:00

URL: https://externals.io/message/125183#125188 · In reply to: #125185

>
> This is a feature I've wanted for a very long time! The RFC is very 
> straight forward, and the appendix does a great job of enumerating the 
> possible expressions.
>
> Nice work all around!

Thanks, Matt! Glad you like this one (and the last one!)

Hopefully we can land it this time 🙂

Cheers,
Bilge

---

## #125194 — Rob Landers — 2024-08-24T21:14:52+00:00

URL: https://externals.io/message/125183#125194 · In reply to: #125183

> Hi gang,
> 
> New RFC just dropped: https://wiki.php.net/rfc/default_expression. I 
> think some of you might enjoy this one. Hit me with any feedback.
> 
> This one already comes complete with working implementation that I've 
> been cooking for a little while. Considering I don't know C or PHP 
> internals, one might think implementing this feature would be 
> prohibitively difficult, but considering the amount of help and guidance 
> I received from Ilija, Bob and others, it would be truer to say it would 
> have been more difficult to fail! Huge thanks to them.
> 
> Cheers,
> Bilge
> 

This is pretty awesome! I see this as some syntax sugar, to be honest:

> as soon as two or more nullable arguments are involved

I'm not sure what you mean here. I use this method all the time :) much to the chagrin of some of my coworkers.

function stuff($foo = 'bar', $baz = 'world');

stuff(...[ ...($foo ? ['foo' => $foo] : []), ...($baz ? ['baz' => $baz] : [])]);

Having this would be a lot less verbose.

— Rob

---

## #125195 — Bilge — 2024-08-24T21:35:57+00:00

URL: https://externals.io/message/125183#125195 · In reply to: #125194

> 
>
> > […]
>
> I'm not sure what you mean here. I use this method all the time :) 
> much to the chagrin of some of my coworkers.
>
> function stuff($foo = 'bar', $baz = 'world');
>
> stuff(...[ ...($foo ? ['foo' => $foo] : []), ...($baz ? ['baz' => 
> $baz] : [])]);
>
You're right; splat with keys does work. This is something my RFC 
currently glosses over and should probably be rectified!

Cheers,
Bilge

---

## #125200 — Mike Schinkel — 2024-08-25T02:41:27+00:00

URL: https://externals.io/message/125183#125200 · In reply to: #125194

>   
> 
>   
>   
>   
> I'm not sure what you mean here. I use this method all the time :) much to the chagrin of some of my coworkers.
>   
>
>   
> function stuff($foo = 'bar', $baz = 'world');
>   
>
>   
> stuff(...[ ...($foo ? ['foo' =>  $foo] : []), ...($baz ? ['baz' =>  $baz] : [])]);
>   
>   
  

  
 And you are one who complains about gotos!    😲
  
  
-Mike

---

## #125201 — Ben Ramsey — 2024-08-25T03:06:05+00:00

URL: https://externals.io/message/125183#125201 · In reply to: #125183

> 
> 
> Hi gang,
> 
> New RFC just dropped: https://wiki.php.net/rfc/default_expression. I think some of you might enjoy this one. Hit me with any feedback.
> 
> This one already comes complete with working implementation that I've been cooking for a little while. Considering I don't know C or PHP internals, one might think implementing this feature would be prohibitively difficult, but considering the amount of help and guidance I received from Ilija, Bob and others, it would be truer to say it would have been more difficult to fail! Huge thanks to them.
> 
> Cheers,
> Bilge


Great RFC, Bilge! I was already on-board after the introduction, but if I had any doubts, the examples in the appendix sold me.

Cheers,
Ben

---

## #125203 — Rob Landers — 2024-08-25T07:00:29+00:00

URL: https://externals.io/message/125183#125203 · In reply to: #125200

> 
> 
> > […]
> 
> And you are one who complains about gotos!  😲
> 
> -Mike

Haha, there is a difference between production/professional code and internal tools. Internal tools are a place to experiment and have a little fun, IMHO.

— Rob

---

## #125205 — Bilge — 2024-08-25T08:39:26+00:00

URL: https://externals.io/message/125183#125205 · In reply to: #125201

> Great RFC, Bilge! I was already on-board after the introduction, but if I had any doubts, the examples in the appendix sold me.
>
> Cheers,
> Ben

Thanks, Ben. That means a lot to me :)

Cheers,
Bilge

---

## #125206 — Juliette Reinders Folmer — 2024-08-25T09:49:42+00:00

URL: https://externals.io/message/125183#125206 · In reply to: #125183

(resending as I accidentally originally send a private reply instead of 
sending the below to the list)


> Hi gang,
>
> New RFC just dropped: https://wiki.php.net/rfc/default_expression. I 
> think some of you might enjoy this one. Hit me with any feedback.
>
> This one already comes complete with working implementation that I've 
> been cooking for a little while. Considering I don't know C or PHP 
> internals, one might think implementing this feature would be 
> prohibitively difficult, but considering the amount of help and 
> guidance I received from Ilija, Bob and others, it would be truer to 
> say it would have been more difficult to fail! Huge thanks to them.
>
> Cheers,
> Bilge
>

Hi Bilge,

I like the idea, but see some potential for issues with ambiguity, which 
I don't see mentioned in the RFC as "solved".

Example 1:
```php
function foo($paramA, $default = false) {}
foo( default: default ); // <= Will this be handled correctly ?
```

Example 2:
```php
callme(
     match($a) {
         10 => $a * 10,
         20 => $a * 20,
         default => $a * default, // <= Based on a test in the PR this 
should work. Could you confirm ?
     }
);
```

Example 3:
```php
switch($a) {
     case 'foo':
         return callMe($a, default); // I presume this shouldn't be a 
problem, but might still be good to have a test for this ?
     default:
         return callMe(10, default); // I presume this shouldn't be a 
problem, but might still be good to have a test for this ?
}
```

On that note, might it be an idea to introduce a separate token for the 
`default` keyword when used as a default expression in a function call 
to reduce ambiguity ?

Smile,
Juliette

---

## #125207 — Bilge — 2024-08-25T10:01:38+00:00

URL: https://externals.io/message/125183#125207 · In reply to: #125206

> (resending as I accidentally originally send a private reply instead 
> of sending the below to the list)
>
> 
> > […]
>
> Hi Bilge,
Hi :)
> I like the idea, but see some potential for issues with ambiguity, 
> which I don't see mentioned in the RFC as "solved".
>
> Example 1:
> ```php
> function foo($paramA, $default = false) {}
> foo( default: default ); // <= Will this be handled correctly ?
> ```

No, but not because of my RFC, but because $paramA is a required 
parameter that was not specified. Assuming that was just a typo, the 
following works as expected:

function foo($paramA = 1, $default = false) {
     var_dump($default);
}
foo(default: default); // bool(false)

> Example 2:
> ```php
> callme(
>     match($a) {
>         10 => $a * 10,
>         20 => $a * 20,
>         default => $a * default, // <= Based on a test in the PR this 
> should work. Could you confirm ?
>     }
> );
> ```
Yes.
> Example 3:
> ```php
> switch($a) {
>     case 'foo':
>         return callMe($a, default); // I presume this shouldn't be a 
> problem, but might still be good to have a test for this ?
>     default:
>         return callMe(10, default); // I presume this shouldn't be a 
> problem, but might still be good to have a test for this ?
> }
> ```
Yes.
> On that note, might it be an idea to introduce a separate token for 
> the `default` keyword when used as a default expression in a function 
> call to reduce ambiguity ?
Considering the Bison grammar compiles, I believe there can be no 
ambiguity. I specifically picked `default` because I think it is the 
most intuitive keyword to use for this, and it's conveniently already a 
reserved word.

Cheers,
Bilge

---

## #125208 — Rob Landers — 2024-08-25T10:37:16+00:00

URL: https://externals.io/message/125183#125208 · In reply to: #125207

> 
> > […]
> Hi :)
> > […]
> 
> No, but not because of my RFC, but because $paramA is a required 
> parameter that was not specified. Assuming that was just a typo, the 
> following works as expected:
> 
> function foo($paramA = 1, $default = false) {
>     var_dump($default);
> }
> foo(default: default); // bool(false)
> 
> > […]
> Yes.
> > […]
> Yes.
> > […]
> Considering the Bison grammar compiles, I believe there can be no 
> ambiguity. I specifically picked `default` because I think it is the 
> most intuitive keyword to use for this, and it's conveniently already a 
> reserved word.

Other tools parse the tokens directly (for example, I have a tool to take php classes and convert them to graphql specifications. It parses the tokens emitted the tokenization extension), and having "default" tokens in unexpected places presents an ambiguity and BC break for those tools. By having a token (DEFAULT_PARAM_VALUE) or something to disambiguate might be better. I had assumed it was a separate token when I first read it, so this is a good point.

— Rob

---

## #125209 — Larry Garfield — 2024-08-25T13:35:14+00:00

URL: https://externals.io/message/125183#125209 · In reply to: #125183

> Hi gang,
>
> New RFC just dropped: https://wiki.php.net/rfc/default_expression. I 
> think some of you might enjoy this one. Hit me with any feedback.
>
> This one already comes complete with working implementation that I've 
> been cooking for a little while. Considering I don't know C or PHP 
> internals, one might think implementing this feature would be 
> prohibitively difficult, but considering the amount of help and guidance 
> I received from Ilija, Bob and others, it would be truer to say it would 
> have been more difficult to fail! Huge thanks to them.
>
> Cheers,
> Bilge

I am still not fully sold on this, but I like it a lot better than the previous attempt at a default keyword.  It's good that you mention named arguments, as those do replace like 95% of the use cases for "put default here" in potential function calls, and the ones it doesn't, you call out explicitly as the justification for this RFC.

The approach here seems reasonable overall.  The mental model I have from the RFC is "yoink the default value out of the function, drop it into this expression embedded in the function call, and let the chips fall where they may."  Is that about accurate?

My main holdup is the need.  I... can't recall ever having a situation where this is something I needed.  Some of the examples show valid use cases (eg, the "default plus this binary flag" example), but again, I've never actually run into that myself in practice.

My other concern is the list of supported expression types.  I understand how the implementation would naturally make all of those syntactically valid, but it seems many of them, if not most, are semantically nonsensical.  Eg, `default > 1` would take a presumably numeric default value and output a boolean, which should really never be type compatible with the function being called.  (A param type of int|bool is a code smell at best, and a fatal waiting to happen at worst.)  In practice, I think a majority of those expressions would be logically nonsensical, so I wonder if it would be better to only allow a few reasonable ones and block the others, to keep people from thinking nonsensical code would do something useful.

--Larry Garfield

---

## #125210 — John Coggeshall — 2024-08-25T14:04:20+00:00

URL: https://externals.io/message/125183#125210 · In reply to: #125183

> Hi gang,
>
> New RFC just dropped: https://wiki.php.net/rfc/default_expression. I
> think some of you might enjoy this one. Hit me with any feedback.
>

Seems like you are missing an option for your theme example, which would be to simply extend the Config class?
While I can see the value in the concept of default , I think it's a mistake to allow default to be used as a generic operand as shown in the RFC appendix. It seems to me that the whole point of something like default is to not have to worry about what the upstream API wanted for that value, but the second you start allowing operations where default is an operand you've reintroduced the problem you were trying to avoid if the upstream API were to change the type of what default ultimately resolves to. Worse actually because now I have no idea what default is when I read code without having to dig up the upstream API.
Other thoughts here are what happens when default resolves to an object or enumeration or something complex? Your original example had CuteTheme , so can you call a method of default ?? I could entirely see someone doing something like this for example:
enum Foo:string {
// cases

public function buildSomeValidBasedOnCase(): int { // ... }
}

F(MyClass::makeBasedOnValue(default->buildSomeValidBasedOnCase()))
IMO most operators listed in the appendix should be disallowed. I can see the value of default | JSON_PRETTY_PRINT, but I am pretty strongly opposed to the idea of introducing a "conditional based on the default value of an upstream API call" concept of default >=1 .

---

## #125211 — Rob Landers — 2024-08-25T14:08:35+00:00

URL: https://externals.io/message/125183#125211 · In reply to: #125209

> 
> > […]
> 
> I am still not fully sold on this, but I like it a lot better than the previous attempt at a default keyword.  It's good that you mention named arguments, as those do replace like 95% of the use cases for "put default here" in potential function calls, and the ones it doesn't, you call out explicitly as the justification for this RFC.
> 
> The approach here seems reasonable overall.  The mental model I have from the RFC is "yoink the default value out of the function, drop it into this expression embedded in the function call, and let the chips fall where they may."  Is that about accurate?
> 
> My main holdup is the need.  I... can't recall ever having a situation where this is something I needed.  Some of the examples show valid use cases (eg, the "default plus this binary flag" example), but again, I've never actually run into that myself in practice.

Potentially the most useful place would be in attributes. Take crell\serde (:p) for instance:

#[SequenceField(implodeOn: default . ' ', joinOn: ' ' . default . ' ')]

Where you may just want it to be a little more readable, but aren't interested in the default implosion. In attributes, it has to be a static expression and I think this passes that test? At least that is one place I would find most useful.

Then there are things like the example I gave before, where you need to call some library code as library code and pass through the intentions. It also gets us one step closer to something like these shenanigans:

function configureSerializer(Serde $serializer = new SerdeCommon(formatters: default as $formatters));

Where we can call configureSerializer(formatters: new JsonStreamFormatter()).

Some pretty interesting stuff.

> 
> My other concern is the list of supported expression types.  I understand how the implementation would naturally make all of those syntactically valid, but it seems many of them, if not most, are semantically nonsensical.  Eg, `default > 1` would take a presumably numeric default value and output a boolean, which should really never be type compatible with the function being called.  (A param type of int|bool is a code smell at best, and a fatal waiting to happen at worst.)  In practice, I think a majority of those expressions would be logically nonsensical, so I wonder if it would be better to only allow a few reasonable ones and block the others, to keep people from thinking nonsensical code would do something useful.

I'm reasonably certain you can write nonsensical PHP without this feature. I don't think we should be the nanny of developers.

— Rob

---

## #125212 — Matthew Weier O'Phinney — 2024-08-25T14:33:12+00:00

URL: https://externals.io/message/125183#125212 · In reply to: #125210

>
>
> 
>
> Hi gang,
>
> New RFC just dropped: https://wiki.php.net/rfc/default_expression. I
> think some of you might enjoy this one. Hit me with any feedback.
>
>
> Seems like you are missing an option for your theme example, which would
> be to simply extend the Config  class?
>
> While I can see the value in the concept of default , I think it's a
> mistake to allow default  to be used as a generic operand as shown in the
> RFC appendix. It seems to me that the whole point of something like
> default  is to not have to worry about what the upstream API wanted for
> that value, but the second you start allowing operations where default
> is an operand you've reintroduced the problem you were trying to avoid if
> the upstream API were to change the type of what default  ultimately
> resolves to. Worse actually because now I have no idea what default  is
> when I read code without having to dig up the upstream API.
>

If the underlying API changes the argument type, consumers will have an
issue regardless. For those cases where the expression is simply `default`,
you'd actually be protected from the API change, which is a net benefit
already.

This also protects the user from changes in the argument names.


> Other thoughts here are what happens when default  resolves to an object
> or enumeration or something complex? Your original example had CuteTheme ,
> so can you call a method of default ?? I could entirely see someone doing
> something like this for example:
>
> enum Foo:string {
>     // cases
>
>     public function buildSomeValidBasedOnCase(): int { // ... }
> }
>
> F(MyClass::makeBasedOnValue(default->buildSomeValidBasedOnCase()))
>
> IMO most operators listed in the appendix should be disallowed. I can see
> the value of default | JSON_PRETTY_PRINT,  but I am pretty strongly
> opposed to the idea of introducing a "conditional based on the default
> value of an upstream API call" concept of default >=1 .

---

## #125213 — John Coggeshall — 2024-08-25T14:58:14+00:00

URL: https://externals.io/message/125183#125213 · In reply to: #125212

> If the underlying API changes the argument type, consumers will have an issue regardless. For those cases where the expression is simply `default`, you'd actually be protected from the API change, which is a net benefit already.
>
> This also protects the user from changes in the argument names.

As I said, I don't have a particular problem with default as a keyword to express "whatever the default value might be in the function declaration", but I do have some real concerns about its use as an operand in an expression. The RFC provides for a single valid use case of operators (i.e. things like default | `JSON_PRETTY_PRINT` ), yet calls for a huge array of valid operations, many of which the RFC itself notes don't make much / any sense. I'd personally like to see this RFC dramatically reduce the scope of operations supported with default as an operand initially (e.g. perhaps only bitwise ops), and revisit additional operations as needed down the road. IMO there is a very small subset of all PHP operators that make any sense at all in this context, and even fewer that I think are a good idea to allow even if they might make some sort of sense.

---

## #125214 — Rob Landers — 2024-08-25T15:11:07+00:00

URL: https://externals.io/message/125183#125214 · In reply to: #125213

> 
> 
> > […]
> 
> As I said, I don't have a particular problem with `default`  as a keyword to express "whatever the default value might be in the function declaration", but I do have some real concerns about its use as an operand in an expression. The RFC provides for a single valid use case of operators (i.e. things like `default | JSON_PRETTY_PRINT` ), yet calls for a huge array of valid operations,  many of which the RFC itself notes don't make much / any sense. I'd personally like to see this RFC dramatically reduce the scope of operations supported with `default`  as an operand initially (e.g. perhaps only bitwise ops), and revisit additional operations as needed down the road. IMO there is a very small subset of all PHP operators that make any sense at all in this context, and even fewer that I think are a good idea to allow even if they might make some sort of sense.

Which operants don’t make sense?

— Rob

---

## #125216 — John Coggeshall — 2024-08-25T15:28:56+00:00

URL: https://externals.io/message/125183#125216 · In reply to: #125214

>
> Which operants don’t make sense?
Well certainly all of the ones toward the end of the appendix in the RFC the RFC itself notes are non-sensical. Personally, I'm not sold on the idea default should be an operand in an expression at all though.
I do see the value of bitwise operators / expressions as highlighted in the RFC. There might be other cases, but I'm wary of them -- I don't think giving developers the ability to write logic and expressions against hard-coded default values in upstream APIs has a lot of merit in most cases. I can tell you I doubt I would ever support the idea of calling methods of default , which I'm still unclear if this RFC is proposing.
I would suggest a compromise where the RFC be refocused to those expressions and operators that explicitly make sense, and leave the rest of them out for now. To me, that basically means "default with bitwise expression support" based on what I've seen so far.
John

---

## #125217 — Bilge — 2024-08-25T15:29:07+00:00

URL: https://externals.io/message/125183#125217 · In reply to: #125209

> The approach here seems reasonable overall.  The mental model I have from the RFC is "yoink the default value out of the function, drop it into this expression embedded in the function call, and let the chips fall where they may."  Is that about accurate?
Yes, as it happens. That is the approach we took, because the 
alternative would have been changing how values are sent to functions, 
which would have required a lot more changes to the engine with no clear 
benefit. Internally it literally calls the reflection API, but a 
low-level call, that elides the class instantiation and unnecessary 
hoops of the public interface that would just slow it down.
> My main holdup is the need.  I... can't recall ever having a situation where this is something I needed.  Some of the examples show valid use cases (eg, the "default plus this binary flag" example), but again, I've never actually run into that myself in practice.
That's fine. Not everyone will have such a need, and of those that do, 
I'm willing to bet it will be rare or uncommon at best. But for those 
times it is needed, the frequency by which it is needed in no way 
diminishes its usefulness.I rarely use `goto` but that doesn't mean we 
shouldn't have the feature.
> My other concern is the list of supported expression types.  I understand how the implementation would naturally make all of those syntactically valid, but it seems many of them, if not most, are semantically nonsensical.  Eg, `default > 1` would take a presumably numeric default value and output a boolean, which should really never be type compatible with the function being called.  (A param type of int|bool is a code smell at best, and a fatal waiting to happen at worst.)  In practice, I think a majority of those expressions would be logically nonsensical, so I wonder if it would be better to only allow a few reasonable ones and block the others, to keep people from thinking nonsensical code would do something useful.

Since you're not the only one raising this, I will address it, but just 
to say there is no good reason, in my mind, to ever prohibit the 
expressiveness. To quote Rob

 >I'm reasonably certain you can write nonsensical PHP without this 
feature. I don't think we should be the nanny of developers.

I fully agree with that sentiment. It seems to be biting me that I went 
to the trouble of listing out every permutation of what /expression/ 
means where perhaps this criticism would not have been levied at all had 
I chosen not to do so. Why does that matter? Because PHP already allows 
you to do many more ridiculous things, they're just not routinely 
presented to you so they're not part of your mind map. The end-user 
documentation will also not mention the nonsense cases, so the average 
developer will not think of them. You can write, `include(1 + 1);`, 
because `include()` accepts an expression. You will get: "Failed opening 
'2' for inclusion". Should we restrict that? No, because that's just how 
expressions work in any context where they're allowed. Special-casing 
the `T_DEFAULT` grammar would not only bloat the grammar rules but also 
increase the chance that new expression grammars introduced in future, 
which could conveniently interoperate with `default`, would be 
unintentionally excluded by omission.

Cheers,
Bilge

---

## #125218 — Rowan Tommins [IMSoP] — 2024-08-25T15:31:46+00:00

URL: https://externals.io/message/125183#125218 · In reply to: #125209

> My other concern is the list of supported expression types.  I
> understand how the implementation would naturally make all of those
> syntactically valid, but it seems many of them, if not most, are
> semantically nonsensical.


I tend to agree with Larry and John that the list of operators should be 
restricted - we can always allow more in future, but restricting later 
is much harder.

A few rules that seem logical to me:

1) The expression should be reasonably guaranteed to produce the same 
type as the actual default.

- No casts
- No comparison operators, because they produce booleans from 
non-boolean input
- No "<=>". Technically, it has an integer result, but it's rare to use 
it as one, rather than a kind of three-value boolean
- No "instanceof"
- No "empty"

2) The expression should not have side effects (outside of exotic 
operator overloads).

- No "include", "require", etc
- No "throw"
- No "print"
- Borderline, but I would also say no "clone"

3) The expression should be passing additional information into the 
function, not pulling information out of it. The syntax shouldn't be a 
way to write obfuscated reflection, or invert data flow from callee to 
caller.

- No assignments.
- No ternaries with "default" on the left-hand side - "$foo ? $bar : 
default" is acting on local knowledge, but "default ? $foo : $bar" is 
acting on information the caller shouldn't know
- Same for "?:" and "??"
- No "match" with "default" as the condition or branch, for the same 
reason. "match($foo) { $bar => default }" is fine, match(default) { ... 
}" or "match($foo) { default => ... }" are not.

Note that these can be seen as aspects of the same rule: the aim of the 
expression should be to transform the default value into another value 
of the same type, not to pull it out and perform arbitrary operations 
based on it.


I believe that leaves us with:

- Arithmetic operators: binary + - * / % **, unary + -
- Bitwise operators: & | ^ << >>  ~
- Boolean operators: && || and or xor !
- Conditions with default on the RHS: $foo ? $bar : default, $foo ?: 
default, $foo ?? default, match($foo) { $bar => default }
- Parentheses: (((default)))


Even then, I look at that list and see more problems than use cases. As 
the RFC points out, library authors already worry about the maintenance 
burden of named argument support, will they now also need to question 
whether someone is relying on "default + 1" having some specific effect?

Maybe we should instead require justification for each addition:

- Bitwise | is nicely demonstrated in the RFC
- Bitwise & could probably be justified on similar grounds
- "$foo ? $bar : default" is discussed in the RFC
- The other "conditions with default on the RHS" in my shortlist above 
fit the same basic use case

Beyond that, I'm struggling to think of meaningful uses: "whatever the 
function sets as its default, do the opposite"; "whatever number the 
function sets as default, raise it to the power of 3"; etc. Again, they 
can easily be added in later versions, if a use case is pointed out.


Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #125219 — Bilge — 2024-08-25T15:42:56+00:00

URL: https://externals.io/message/125183#125219 · In reply to: #125210

> Other thoughts here are what happens when |default| resolves to an 
> object or enumeration or something complex? Your original example had 
> |CuteTheme| , so can you call a method of |default| ?? I could 
> entirely see someone doing something like this for example:
>
> enum Foo:string {
>     // cases
>
>     public function buildSomeValidBasedOnCase(): int { // ... }
> }
>
> F(MyClass::makeBasedOnValue(default->buildSomeValidBasedOnCase()))

As you have written it, no, you will get a parser error: Parse error: 
syntax error, unexpected token "->", expecting ")"

However, you can wrap the `default` in parens as in the following example:

class C {
     function F() {
         echo 'lol';
     }
}

function G($V = new C) {}

G((default)->F()); // lol

---

## #125220 — Rob Landers — 2024-08-25T15:54:48+00:00

URL: https://externals.io/message/125183#125220 · In reply to: #125218

> 
> > […]
> 
> 
> I tend to agree with Larry and John that the list of operators should be restricted - we can always allow more in future, but restricting later is much harder.
> 
> A few rules that seem logical to me:
> 
> 1) The expression should be reasonably guaranteed to produce the same type as the actual default.
> 
> 
> - No casts
> - No comparison operators, because they produce booleans from non-boolean input
> - No "<=>". Technically, it has an integer result, but it's rare to use it as one, rather than a kind of three-value boolean
> - No "instanceof"
> - No "empty"
> 
> 2) The expression should not have side effects (outside of exotic operator overloads).
> 
> 
> - No "include", "require", etc
> - No "throw"
> - No "print"
> - Borderline, but I would also say no "clone"
> 
> 3) The expression should be passing additional information into the function, not pulling information out of it. The syntax shouldn't be a way to write obfuscated reflection, or invert data flow from callee to caller.
> 
> 
> - No assignments.
> - No ternaries with "default" on the left-hand side - "$foo ? $bar : default" is acting on local knowledge, but "default ? $foo : $bar" is acting on information the caller shouldn't know
> - Same for "?:" and "??"
> - No "match" with "default" as the condition or branch, for the same reason. "match($foo) { $bar => default }" is fine, match(default) { ... }" or "match($foo) { default => ... }" are not.
> 
> Note that these can be seen as aspects of the same rule: the aim of the expression should be to transform the default value into another value of the same type, not to pull it out and perform arbitrary operations based on it.
> 
> 
> 
> I believe that leaves us with:
> 
> 
> - Arithmetic operators: binary + - * / % **, unary + -
> - Bitwise operators: & | ^ << >>  ~
> - Boolean operators: && || and or xor !
> - Conditions with default on the RHS: $foo ? $bar : default, $foo ?: default, $foo ?? default, match($foo) { $bar => default }
> - Parentheses: (((default)))
> 
> 
> 
> Even then, I look at that list and see more problems than use cases. As the RFC points out, library authors already worry about the maintenance burden of named argument support, will they now also need to question whether someone is relying on "default + 1" having some specific effect?
> 
> Maybe we should instead require justification for each addition:
> 
> 
> - Bitwise | is nicely demonstrated in the RFC
> - Bitwise & could probably be justified on similar grounds
> - "$foo ? $bar : default" is discussed in the RFC
> - The other "conditions with default on the RHS" in my shortlist above fit the same basic use case
> 
> Beyond that, I'm struggling to think of meaningful uses: "whatever the function sets as its default, do the opposite"; "whatever number the function sets as default, raise it to the power of 3"; etc. Again, they can easily be added in later versions, if a use case is pointed out.
> 
> 
> 
> Regards,
> 
> -- 
> Rowan Tommins
> [IMSoP]

Hi Rowan, you went through a lot of trouble to write this out, and the reasoning makes sense to me. However, all the nonsensical things you say shouldn’t be allowed are already perfectly allowed today, you just have to type a bunch of boilerplate reflection code. There is no new behavior here, just new syntax. 

— Rob

---

## #125221 — Rowan Tommins [IMSoP] — 2024-08-25T16:05:54+00:00

URL: https://externals.io/message/125183#125221 · In reply to: #125217

> You can write, `include(1 + 1);`, because `include()` accepts an 
> expression. You will get: "Failed opening '2' for inclusion". Should 
> we restrict that? No, because that's just how expressions work in any 
> context where they're allowed.


I think a better comparison might be the "new in initializers" and 
"fetch property in const expressions" RFCs, which both forbid uses which 
would naturally be allowed by the grammar. The rationale in those cases 
was laid out in 
https://wiki.php.net/rfc/new_in_initializers#unsupported_positions and 
https://wiki.php.net/rfc/fetch_property_in_const_expressions#supporting_all_objects

To pull out a point that might be overlooked at the bottom of my longer 
response earlier:

 > As the RFC points out, library authors already worry about the 
maintenance burden of named argument support, will they now also need to 
question whether someone is relying on "default + 1" having some 
specific effect?

By saying "default can be used in any expression, as complex as the 
caller can imagine", we're implicitly saying "if you add a default to 
your function signature, that is no information a user can pull *out* as 
part of your API".

Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #125222 — Rowan Tommins [IMSoP] — 2024-08-25T16:21:01+00:00

URL: https://externals.io/message/125183#125222 · In reply to: #125220

> Hi Rowan, you went through a lot of trouble to write this out, and the 
> reasoning makes sense to me. However, all the nonsensical things you 
> say shouldn’t be allowed are already perfectly allowed today, you just 
> have to type a bunch of boilerplate reflection code. There is no new 
> behavior here, just new syntax. 


Firstly, your response to John was essentially "please give more 
details" [https://externals.io/message/125183#125214], and your response 
to me is "thanks for the details, but I'm not going to engage with 
them". That's a bit frustrating.

Secondly, I don't think "it's possible with half a dozen lines of 
reflection, so it's fine for it to be a first-class feature of the 
language syntax" is a strong argument. The Reflection API is a bit like 
the Advanced Settings panel in a piece of software, it comes with a big 
"Proceed with Caution" warning. You only move something from that 
Advanced Settings panel to the main UI when it's going to be commonly 
used, and generally safe to use. I don't think allowing arbitrary 
operations on a value that's declared as the default of some other 
function passes that test.

Regards,

-- 
Rowan Tommins
[IMSoP]

---

Next page: https://externals.io/message/125183.md?page=2
