Skip to content

Not-Null pipe operator #180

Open
Open
@thekid

Description

@thekid

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions