Skip to content

pattern matching generic case classes might be too strict #2675

Closed
@rkuhn

Description

@rkuhn

Consider the following example:

class Ref[-T]

sealed trait Op
case class Send[T](ref: Ref[T], msg: T) extends Op

object Main {
  val ref: Ref[String] = ???
  val op: Op = Send(ref, "hello")
  op match {
    case Send(ref, msg) => 
  }
}

Compiling this with dotty 0.1.2-RC1 yields:

[warn] -- Warning: /Users/rkuhn/comp/test/dotty/dotty-project-template/src/main/scala/Main.scala:10:13 
[warn] 10 |    case Send(target, msg) => 
[warn]    |         ^^^^^^^^^^^^^^^^^
[warn]    |         There is no best instantiation of pattern type Send[Any^]
[warn]    |         that makes it a subtype of selector type Op.
[warn]    |         Non-variant type variable T cannot be uniquely instantiated.
[warn]    |         (This would be an error under strict mode)
[warn] one warning found

The important knowledge encapsulated by the Send type is that it has been constructed with ref and msg matching each other. When pattern matching on such a type, I don’t care what the type parameter was, I only care that it was the same for the ref and the msg. Why can’t dotty just make up a type parameter that is not bound and use that in the case statement?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions