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
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
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.
I'm not totally sold on the idea, but I can definitely see why it would
be useful.
I think a more complex example might show it more clearly:
function frobulate(int $id, int $flags = FROB_CLOCKWISE |
FROB_MEDIUM_SPEED) {
// ...
}
Now try to write a function that wraps this in some way, and wants to
retain the default for the $flags parameter without hard-coding it. In
order to get the default, you have to call the function with fewer
arguments:
function log_and_frobulate(int $id, int $flags = null) {
log_stuff($id);
if ( $flags === null ) {
return frobulate($id);
} else {
return frobulate($id, $flags);
}
}
(There's probably other approaches using the "..." argument unpacking
operator, but off the top of my head, I can't think of any that avoid
the if completely.)
If the argument was specified with $flags??=... instead of $flags=...
the wrapper doesn't need the extra branch:
function log_and_frobulate(int $id, int $flags = null) {
log_stuff($id);
return frobulate($id, $flags);
}
Regards,
--
Rowan Tommins
[IMSoP]
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
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
I'd argue that named arguments have rendered the main argument for default
null and void.
(How many puns can he squeeze into one sentence, Bob?)
--Larry Garfield