Skip to content

[Proposal]: Match/Switch #1718

@norberttech

Description

@norberttech

Describe the Proposal

Currently the only way to replicate match/switch behavior is through multiple, nested whenscalar functions.
It works but DX is not great since with each nesting level its getting harder to understand what's going to happen.

We need a better alternative

API Adjustments

Ideally I would see it like this:

match([
    case(ref('string')->contains('-'), ref('string')->strReplace('-', ' ')),
    case(ref('string')->isType(type_numeric_string()), ref('string')->cast(type_integer())),
    case(ref('string')->endsWith('%'), ref('string')->strReplace('%', '')->cast(type_integer())),
    case(ref('string')->startsWith('+'), ref('string')->strReplace('+', '')->cast(type_integer())),
])

but both match and case are reserved php keywords, so will need to think about better alternatives

Are you intenting to also work on proposed change?

Yes

Are you interested in sponsoring this change?

None

Integration & Dependencies

No response

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions