Skip to content

Pattern matching on types having type parameters or type members #1737

Closed
@julienrf

Description

@julienrf

The following code does not compile:

  sealed trait Foo[A]
  case object FooI extends Foo[Int]
  case class FooS(b: Boolean) extends Foo[String]

  def algFoo[A](foo: Foo[A]): A =
    foo match {
      case FooI => 42
      case FooS(b) => "foo"
    }
  sealed trait Bar { type Out }
  case object BarI extends Bar { type Out = Int }
  case class BarS(b: Boolean) extends Bar { type Out = String }

  def algBar(bar: Bar): bar.Out =
    bar match {
      case BarI => 42
      case BarS(b) => "bar"
    }

The errors are:

[error] -- [E007] Type Mismatch Error: -------------------
[error] 18 |      case FooI => 42
[error]    |                   ^^
[error]    |                   found:    Int(42)
[error]    |                   required: A
[error]    |                   
[error] -- [E007] Type Mismatch Error: -------------------
[error] 28 |      case BarI => 42
[error]    |                   ^^
[error]    |                   found:    Int(42)
[error]    |                   required: bar.Out
[error]    |                   
[error] -- [E007] Type Mismatch Error: -------------------
[error] 29 |      case BarS(b) => "bar"
[error]    |                      ^^^^^
[error]    |                      found:    String("bar")
[error]    |                      required: bar.Out
[error]    |                      
[error] three errors found

The first example (with Foo) compiles fine with scalac. In Dotty, the error is raised only in the case of a case object, not a case class.
The second example (with Bar) fails to compile with both scalac and dotc.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions