Skip to content

Discrepancy in pattern matching type checking between dotty and scalac #1269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
liufengyun opened this issue May 20, 2016 · 1 comment
Closed
Assignees

Comments

@liufengyun
Copy link
Contributor

Following code compiles in scalac, but in dotty it generates following error message:

error: Pattern type Patmat.this.module.O.LetR is neither a subtype nor a supertype of selector type Patmat.this.module.Tree
    case module.O.LetR() =>
trait Module {
  sealed abstract class Tree

  case class LetL() extends Tree

  object O {
    case class LetR() extends Tree
  }
}

class Patmat(val module: Module) {
  def patmat(tree: module.Tree) = tree match {
    case module.LetL() =>
    case module.O.LetR() =>
  }
}
@Blaisorblade
Copy link
Contributor

Took a look, and I'm convinced the behavior is undefensible.

The given snippet not only compiles in scalac, but also makes some sense, and the error is false: LetR is a subtype of Tree.

The problem is that Tree is sealed, but inheriting classes can be created in different contexts in the same file. (In stranger cases, that has quite weird consequences — you can define alternatives that you can't match on and that might not exist).

So, either LetR should be forbidden because of sealed, if this code is now undesired, or the pattern matcher should be fixed to allow this code.

About the feature itself: I happened to create code like this recently, though I didn't expect it to work, and it turned out to be nice (in specific scenarios), where I wanted separate modules in the same file to add alternatives to my ADT. However, that seem a very weird feature.

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

No branches or pull requests

3 participants