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
"type mismatch" error since Scala 2.13.0-M4 #10960
Comments
Failed previously under The correct idiom is now Also,
I know everyone has |
What should be the recommend rewrite here? package example
sealed trait Maybe[+A] {
def map[B](f: A => B): Maybe[B] = this match {
case Just(a) => Just(f(a))
case a: Empty.type => a
}
}
case class Just[A](value: A) extends Maybe[A]
case object Empty extends Maybe[Nothing] or sealed trait Maybe[+A] {
def map[B](f: A => B): Maybe[B] = this match {
case Just(a) => Just(f(a))
case Empty => Empty
}
}
case class Just[A](value: A) extends Maybe[A]
case object Empty extends Maybe[Nothing] ? |
The first idiom is eq and a check cast, which I think is the usual intuition for matching a singleton. The second idiom is equals, with Probably scalafix should generate Now I don't remember when I started the quinoa. |
A scalafix rewrite should preserve the meaning of the existing code if at all possible. In this case, this means: case Empty =>
val a = this.asInstanceOf[this.type with Empty.type]
a Yes, it's unsound, which precisely matches how unsound the previous meaning was. |
When I asked "What should be the recommend rewrite", I didn't necessarily mean the mechanical ones, but in general, I feel like we should try to conserve the likely-intent of the code, not the surprising behavior of the code. The most straightforward read of the code this match {
//...
case a @ Empty => a
} is that the author tried to pattern match on The point of #1503 is to prevent the users from making unsound assumption. Re-injecting cast would undo the guard. Maybe the best we can do is resurrect the warning message "The value matched by Empty is bound to a" as the error message when types do not match up (not sure it's possible), and suggest A hypothetical error message:
|
I was going to say that if Scalafix doesn't fix something, it's not doing its job. But I like the idea of a lint option to specify whether you prefer improved behavior or reproduced behavior. English "fix" has the primary meaning to set in place. The sense of repair means you have a screw loose, that requires fastening. Nicely, it's related to dike, which is also multivalent, and ditch, Teich. |
bug or by design?
The text was updated successfully, but these errors were encountered: