Skip to content

Complementary case syntax #17781

@som-snytt

Description

@som-snytt

Someone asked about refactoring

case P => if b then r0 else r1

to

case P if b => r0
case P /* if !b */ => r1

It would be nice if the cases were connected by syntax, since they are complementary.

Maybe not

case P else => r1

and maybe not

case P if _ => r1

I am still an advocate for

case if b => r

so I think the problem space is unexplored. Where are the creative EPFL masters students?

In Scala 2, the impetus is to avoid parens around if conditions. But maybe it's also nicer in Scala 3 to split conditional cases as actual cases. Possibly users worry that the desugaring will be less efficient?

An additional desideratum might be to enforce complementarity in the face of intervening cases:

case P if b =>
case Q =>
case P else => // warn if not necessarily reached on P if !b

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions