Skip to content

Automatically reuse pattern variables #1628

@johnynek

Description

@johnynek

consider a function like this:

def if_one_two_none_zero(o: Option[Int]):
    match o:
        case Some(i) if i matches 1: Some(2)
        case Some(i): Some(i)
        case None: Some(0)

we could have written this as:

def if_one_two_none_zero(o: Option[Int]):
    match o:
        case Some(i) if i matches 1: Some(2)
        case Some(_) as some: some
        case None: Some(0)

but it is longer and a bit noisier, but it would avoid an allocation. It would be nice to optimize the first into the second: namely, any values that are identical to parts of the match, just use the reference to the match instead.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions