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
GADTs and top-level type variables #127
Comments
Imported From: https://issues.scala-lang.org/browse/SI-127?orig=1 |
Lauri Alanko (lealanko) said:
No, since T could be a self-type of an instance of Program. |
@Blaisorblade said: |
@paulp said: object Bar {
val x = Program("bippy")
Foo.transform[x.type](x)
} |
Just looked at this again. As pointed out by Paul, Lauri Alanko is right, so this is not really a bug, unlike #5195 and #9879. It's not clear how one change the program to one which should work but exhibits this bug. Instead, it's easy to use a proper GADT, which just works (though the type parameter looks a bit silly), even though it doesn't give the same guarantee. So I'm closing this to avoid confusion. sealed abstract class Tree[T] -- Phantom type parameter. We could add more bounds, but this is enough.
final case class Program(str: String) extends Tree[Program]
final case class Literal(n: Int) extends Tree[Literal]
object Foo {
def transform[T](t: Tree[T]): Tree[T] = t match {
case Program(str) => Program(str + "_")
case Literal(n) => Literal(n + 1)
}
} Could the original program be made to work? Looking at ADTs in other languages, it's not clear there's an existing theory which helps. |
So Cary Robbins at https://gitter.im/typelevel/cats?at=5a82340019147ac3231ccd3e has sketched a way to encode type refinement for a related issue. I’d love to take a closer look; meanwhile, this might help interested users. |
Consider the common case of defining a transformation that preserves the structure of some type:
This fails now with type errors:
Could we do better? Given that {{T <: Program}} on branch 1, and {{Program}} is final, could we infer that {{T == Program}}? This looks like a common case, and a worthwhile ehancement.
The text was updated successfully, but these errors were encountered: