Skip to content

catch-all-with-variable trick needs some clarification #2313

Closed
@ac2epsilon

Description

@ac2epsilon

I suggest adding a note about control structures-match-using default value

  • Using variable bindings, you have to keep in mind, that any form of catch-all effectively breaks the matching chain. Some systems provide lookup for the most specific matches, but Scala's match does not. This error is easier to catch in the case of underscore placeholder but can be easily missed for var bindings. Especially if you suppress or ignore (not dive in) compiler warnings. This notch is a regular source of bad mistakes:
val i = 5
val day = i match
  case 0 => "Sunday"
  case 1 => "Monday"
  case 2 => "Tuesday"
  case 3 => "Wednesday"
  case what => "Guesswho"
  case 4 => "Thursday"
  case 5 => "Friday"
  case 6 => "Saturday"

println(day)

Guesswho

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