Re: [RFC] Null Coalescing Assignment in Method Signature

From: Date: Thu, 21 Jan 2021 22:37:53 +0000
Subject: Re: [RFC] Null Coalescing Assignment in Method Signature
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-112960@lists.php.net to get a copy of this message
On Thu, Jan 21, 2021 at 3:29 PM Larry Garfield <larry@garfieldtech.com>
wrote:
>
> 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.
>

Smells a little like it's verging on the default proposal that was
brought up awhile ago...

function foo(int $a, array $b = [], string $c = '') { ... }
foo(123, default, "bar");

In this case, foo() never wants null as a valid value, but neither does
the caller actually want anything different from the default.

Allowing a null-coalescish sort of initializer is another potential way to
solve this problem, and I'm not here to say I endorse any of them, but
maybe that's the intent.

-Sara


Thread (5 messages)

« previous php.internals (#112960) next »