Re: [RFC] Default expression

From: Date: Sun, 25 Aug 2024 21:09:36 +0000
Subject: Re: [RFC] Default expression
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message


On 25 August 2024 21:00:03 BST, Bilge <[email protected]> wrote:
>class Suspension {
>    /**
>     * @param int $delay Specifies the delay in milliseconds.
>     */
>    public function suspend(int $delay = 1_000) {
>        var_dump($delay);
>    }
>}
>
>class MySuspension extends Suspension {
>    /**
>     * @param float|int|null $delay Specifies the delay in seconds.
>     */
>    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.


Possibly something got lost as you redrafted the example, because as you've written it, neither
the multiplication nor the cast are applied to the value looked up by "default". The
parameter reduces to "(expression) ?: default", which I've already agreed is useful.


I was thinking about why "bitwise or" feels so different from other operators here, and I
realised it's because it's idempotent (I hope I'm using that term correctly): if the
specified bits are already set, it will have no effect.

Consequently, we know that ($x | SOME_FLAG) & SOME_FLAG === SOME_FLAG without knowing the value
of $x. That in turn means that regardless of how the default value changes in future, we know what
"default | JSON_PRETTY_PRINT" will do.

It's as though each bit flag is a separate parameter, and you're saying "pass true to
$prettyPrint, but let the implementation decide sensible defaults for all other flags".

The majority of operators don't have that property, so they require some additional assumptions
about the default, which might not hold in future. For instance, if you use "default + 1",
you are implicitly assuming that the default value is not the maximum allowed value.

Rowan Tommins
[IMSoP]


Thread (101 messages)

« previous php.internals (#125240) next »