Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add match section #21

Merged
merged 1 commit into from
Aug 9, 2022
Merged

Add match section #21

merged 1 commit into from
Aug 9, 2022

Conversation

JesusValeraDev
Copy link
Contributor

📚 Description

Add the match section next to switch.

Copy link
Contributor

@KorvinSzanto KorvinSzanto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be expanded to more specific about more complex match expressions like:

match (true) {
    ...
}

See my example from #3: https://3v4l.org/2s8uK

@JesusValeraDev
Copy link
Contributor Author

I added the following code in the PR:

$returnValue = match ($expr) {
    0 => 'First case',
    1, 2, 3 => multipleCases(),
    default => 'Default case',
};

So, $expr works for anything (true also included).

Which changes do you suggest @KorvinSzanto? 😄

@KorvinSzanto
Copy link
Contributor

KorvinSzanto commented Aug 9, 2022

In my example I'm matching against statements rather than values, in your examples you're only matching values.

Here's another example of matching against statements: https://3v4l.org/5mkYA

function getErrorType(int $code)
{
    return match (true) {
        $code === 418 => ResponseType::Teapot,
        $code >= 100 && $code < 200 => ResponseType::Informational,
        $code >= 200 && $code < 300 => ResponseType::Success,
        $code >= 300 && $code < 400 => ResponseType::Redirect,
        $code >= 400 && $code < 500 => ResponseType::ClientError,
        $code >= 500 && $code < 600 => ResponseType::ServerError,
        default => ResponseType::Unknown,
    };
}

On second thought we can probably just leave this unspecified for now and deal with it in a future version if matches start going off the rails.

@KorvinSzanto KorvinSzanto merged commit 89fbefa into php-fig:master Aug 9, 2022
@JesusValeraDev JesusValeraDev deleted the match branch August 10, 2022 07:40
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants