On Thu, Jan 21, 2021, at 5:51 AM, Daniel Simkus wrote:
> Hello all,
> 
> I am writing to see if anyone is interested in an RFC for ability to 
> automatically coalesce nullable method parameters with the supplied 
> default. It would look something like this:
> 
> function foo(?array $bar ??= []) {
> 
> This would result in $bar automatically converting to an array if null 
> is passed, as opposed to either having to do a null check before the 
> function call and not passing parameter at all - or alternatively 
> having to do something like:
> 
> function foo(?array $bar = []) {
>     $bar ??= [];
> 
> 
> I don’t see this creating any BC incompatibilites and the ??= syntax is 
> something already in use in PHP since 7~
> 
> I don’t have any experience with C, so would ideally love it someone 
> could help with the development with this, or maybe point me in the 
> right direction.
> 
> I would also require RFC Karma if people are interested in opening a 
> discussion about this (dansimkus)
> 
> Thank you all for your time
> 
> Regards,
> Daniel Simkus

I'm unclear why you'd allow null at all then.  If you want $bar to be optional, 
and to be an empty array if not specified, then just do:

function foo(array $bar = []) { ... }

At that point, the only thing adding ?array does is allow you to explicitly 
pass null, presumably because it has some meaning to your function.  If you 
don't want that, don't allow it.

--Larry Garfield

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to