Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add lint check for wildcards in inline match #23642

WojciechMazur started this conversation in Feature Requests
Discussion options

Combining wildcards case _ and inline match can lead to unexpected behaviours when providing runtime computed values.
Here's an example:

inline def stringValue(value: Int): String = {
 inline value match 
 case 1 => "t1"
 case 2 => "t2"
 case _ => "tx"
}
@main def Test =
 val arg = Option(1).get
 println(arg) // 1
 println(stringValue(1)) // "t1"
 println(stringValue(arg)) // "tx"

In this example if we ch about stringValue being computed at compile time we can easily introduce logic bugs to our code base.
To prevent that we need to either:

  • require that value is a cosnt using scala.ompiletime.requireConst
  • not use wildcard match

Both of these have it's own drawbacks, and each of these rules is easy to forget about. I propose we try to introduce a lint check under a flag (e.g. -Winline-match-wildcard) to detect and warn about such usages.

You must be logged in to vote

Replies: 2 comments

Comment options

I wonder if this should actually be the default. Can we ever inline properly with a wildcard present?

You must be logged in to vote
0 replies
Comment options

This is standard behavior of an inline match. If that's not the behavior you want, use a regular match. I don't see how this is different from any other situation where two cases are not disjoint. If want to lint this away, you should lint every non-disjoint pair of cases. But if you do that, inline match does not have any useful behavior that match doesn't have anymore.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /