Open
Description
Proposal
Complement the null-coalescing operator:
$user= $context->userName() ?? 'guest';
Sometimes we want to perform an action if the value is null, and perform an alternative instead:
$user= $context->userName();
return null === $user ? null : strtoupper($user);
We already have a "standard" for chaining expressions with the pipe operator. The above without null handling would be:
return $context->userName() |> stroupper(...);
👉 To accomplish this, this feature request suggests a null-safe pipe operator, which would make the following an equivalent of the above:
return $context->userName() ?|> strtoupper(...);
Consistency
This is consistent with the null-safe object operator. If we rewrote the above using a String
class, we could have the following:
return $context->userName()?->toUpper();
However, wrapping every primitive string in a String
instance would introduce quite a bit of runtime and development overhead!
See also
- https://stackoverflow.com/questions/62929428/opposite-of-nullish-coalescing-operator
- https://wiki.php.net/rfc/pipe-operator
- https://wiki.php.net/rfc/pipe-operator-v2
- Pipe operator, New AST node version php/php-src#7214
- https://docs.hhvm.com/hack/expressions-and-operators/pipe
- Proposal: inverted null-coalesce operator ?! dotnet/roslyn#15823
- https://github.com/tc39/proposal-pipeline-operator
- Optional chaining support for the pipeline operator? tc39/proposal-pipeline-operator#159 - brings up
?>
- Should it be pipeline-expression with more operators [ "|>" | ":|>" | "?|>" | "?:|>" ] tc39/proposal-pipeline-operator#210 - more pipeline operators, includes
?|>
- https://elixirschool.com/en/lessons/basics/pipe_operator
- https://gleam.run/cheatsheets/gleam-for-php-users/#piping
- https://externals.io/message/107661 - anti-coalescing via
!??
- https://externals.io/message/107661#107670 -
?|>
suggested
Activity