Re: [RFC] Default expression

From: Date: Mon, 26 Aug 2024 13:18:04 +0000
Subject: Re: [RFC] Default expression
References: 1 2  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On 26/08/2024 12:55, Jordi Boggiano wrote:
Hey Bilge, Hi :)
On 24.08.2024 18:49, Bilge wrote:
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.
Great work overall, I'm all for it and even though it's not something I saw myself using a whole lot, the json_encode example sold me on it being more useful than I initially thought. Thanks! I concede, this is one of those tools for your toolbox that you will seldom reach for, but comes in very handy whenever you do.
One question (sorry if someone already asked, I scanned the thread but it is getting long..): I don't blame you. I'll summarise the main takeaways in the RFC later.
Taking this example from the RFC:
    function g($p = null) {
        f($p ?? default);
    }
Could you go one step further and use default by default but still allow null to be passed in?
    function g($p = default) {
        f($p);
    }
No. The RFC has a very specific and singular focus in this regard: to permit default /only/ in function call contexts. That is, although default is a valid expression, it cannot be passed around or stored in a variable. Since this is a function definition, rather than a call, this will result in a compiler error. The specific error we get in this case is: "Fatal error: Constant expression contains invalid operations".
Cheers, Bilge

Thread (101 messages)

« previous php.internals (#125271) next »