Skip to content

allow more precise type when matching parametric case classes (or extractors) #8901

Open
@scabug

Description

@scabug

Currently it is possible to match a parametric case class with correct type like so:

case class Register[T](key: Key[T], value: T)
...
x match {
  case r: Register[t] =>
    val k: Key[t] = r.key
    val v: t = r.value // matching types with the key
}

What does not work is the same while extracting values (1):

case Register[t](k, v) => // says “Register[t] does not take parameters”

When not including the name for the type, T is inferred to be Any (2):

case Register(k, v) => // T is Any here, even though Register is not covariant

There are two things which should be done:

  • allow the extraction syntax shown in (1) above
  • implicitly introduce a type with a fresh name when performing the match as in (2), leading to more precise type checking than just assuming Any instead

The second part’s current negative effect can be seen in the following (assuming generic ActorRef[-T] which only accepts messages of type T):

case class Echo[T](msg: T, replyTo: ActorRef[T])
...
x match {
  case Echo(msg, replyTo) =>
    replyTo ! msg // works by accident now because T=Any for both
    replyTo ! 42 // works as well, but should not actually work
}

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