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

Are warnings ok when matching on generic types? #21995

Unanswered
goshacodes asked this question in General Question
Discussion options

Compiler version

3.3.4
"-source:future-migration"

Hi. Compiler generates a warning on this code and I know why, but I'm not sure this is ok.
When you match on any - just cast to Matchable, but in this case it creates a mess with nested matches

Minimized code

sealed trait DiffResult extends Product with Serializable
case class DiffResultAdditional[T](value: T) extends DiffResult
case class Foo()
val diffResult: DiffResult = DiffResultAdditional(1)
diffResult match {
 case DiffResultAdditional(foo: Foo) => println("hello")
 case _ => println("boo")
}

Output

Compiles with warning:

pattern selector should be an instance of Matchable,
but it has unmatchable type T1ドル instead

This works, but it makes your code a mess

diffResult match {
 case DiffResultAdditional(value) =>
 value.asInstanceOf[Matchable] match {
 case foo: Foo => println("hello")
 }
 case _ => println("boo")
}

Expectation

compiles without warning

You must be logged in to vote

Replies: 0 comments

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

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