Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ac2epsilon opened this issue Feb 4, 2022 · 1 comment · Fixed by #2431
Closed

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

ac2epsilon opened this issue Feb 4, 2022 · 1 comment · Fixed by #2431

Comments

@ac2epsilon
Copy link

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
@som-snytt
Copy link
Contributor

At least it warns that subsequent cases are unreachable, but it's nice to warn the user that it will warn. The warning is not necessarily friendly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants