Skip to content

Use named values inside patterns #104

@danilopedraza

Description

@danilopedraza

It can be possible that we want to match a particular value in runtime, but currently that's not possible: any identifier inside a pattern is interpreted like an assignment to that name. Elixir solves this with a prefix operator, specifically the caret (^). I think that we could use a similar solution, even with the same operator (for familiarity).

For example, let's say we have a case expression inside a function:

let f(list, value) :=
    let a := case list do
        [value|tail] => tail
        _ => []

In this case, we are shadowing value and giving it the value of the first element in list. With a caret operator behaving the same as in Elixir, we could do:

let f(list, value) :=
    let a := case list do
        [^value|tail] => tail
        _ => []

and the function would do an entirely different thing: it would return the tail of list only if its first element is equal to value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions