Hacker News new | past | comments | ask | show | jobs | submit login

I use `match` like this a lot now, often with PHP 8.1's enums[1]. It makes it easy to write clean dynamic accessors, without all the extra blocks required with multiple `if` branches:

    /** Check the status of this Task instance. */
    public function status(): Status
    {
        return match (true) {
            $this->inProgress() || $this->isInReview() => Status::Started,
            $this->isApproved() || $this->isRejected() => Status::Finished,
            default                                    => Status::Planned,
        };
    }
[1] https://stitcher.io/blog/php-enums



Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: