Created
April 10, 2011 21:17
-
-
Save milessabin/912730 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See http://lampsvn.epfl.ch/trac/scala/ticket/967. | |
// | |
// Gilles' fix 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment