On 24.08.24 18:49, Bilge wrote:
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

Hello Paul,

I think this is an interesting addition to the language. Personally, I
would replace the full expression list at the end of the RFC with more
examples in real-world scenarios for most of these cases. As far as I
skimmed the discussion, there is some worry of "wrong use" (which I do
not necessarily share). Showing more examples could be useful to focus
on how having default being a full expression gives interesting use
cases, instead of talking about what (in isolation) nonsensical code
people might write.

For me there is another question. When using interfaces and classes,
default values can be introduced, like this:

interface CompressionInterface
{
    public function compress(string $data, int $level): string;
}

class GzipCompression implements CompressionInterface
{
    public function compress(string $data, int $level = 4): string
    {
        // do something
    }
}

When I have the GzipCompression class, I would know there is a default
value for $level, but when using the interface there might or might not
be a default value, depending on the implementation. As far as I read
the RFC, using "default" when there is no default would lead to a
runtime exception, but there is no way of finding out if there is a
default if you do not already know. Being able to test that could be
useful, although I am not sure about the syntax for that. In the example
when getting CompressionInterface, I might test for the existence of a
default value of $level and leave it at the default if there is a
default (maybe I know that some implementations have a default value,
others don't). One could test the specific implementation with
instanceof checks, but the advantage of "default" could be that you do
not need to know the implementation and could only adapt to possibly
defined default values.

Reply via email to