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
Recursive structural types #5355
Comments
Imported From: https://issues.scala-lang.org/browse/SI-5355?orig=1 |
@soc said: |
Dan Rosen (mergeconflict) said: Hey Simon, did you mean to add this to SI-5356? If not, then I totally agree with you ;) |
@milessabin said: As an aside, it's possible to encode your way around the problem if you're really desperate, // Gilles' fix for SI-967 causes the definition of Nat to be rejected with
// the error "Parameter type in structural refinement may not refer to a type
// member of that refinement". However we can work around the problem by
// quantifying out the problematic parameter type and reinstating it via
// a generalized type constraint.
type Num = {
type Rep
val zero : Rep
def succ[R](r : R)(implicit ev : R =:= Rep) : Rep
def show[R](r : R)(implicit ev : R =:= Rep) : String
}
val IntNum : Num = new {
type Rep = Int
val zero = 0
def succ[R](r : R)(implicit ev : R =:= Rep) : Rep = r+1
def show[R](r : R)(implicit ev : R =:= Rep) = r.toString
}
val DoubleNum : Num = new {
type Rep = Double
val zero = 0.0
def succ[R](r : R)(implicit ev : R =:= Rep) : Rep = r+1.0
def show[R](r : R)(implicit ev : R =:= Rep) = r.toString
}
def two(n : Num) = n.show(n.succ(n.succ(n.zero)))
scala> two(IntNum)
res0: String = 2
scala> two(DoubleNum)
res1: String = 2.0 |
@retronym said: |
The pattern match in TypeDiagnostics also matched methods in structural types that do have a return type. Strictly match on vals and defs with an empty TypeTree as return type. Show the default error message otherwise. Fixes scala/bug#5355
There have been a few StackOverflow questions related to recursive structural types, the answer to which is generally "Scala doesn't do that; use typeclasses instead."
Simple example:
I found #3165, but it wasn't clear to me whether this duplicates that issue...
The text was updated successfully, but these errors were encountered: