Created
July 2, 2011 11:26
-
-
Save kmizu/1059948 to your computer and use it in GitHub Desktop.
An implicit conversion that itself requires another implicit conversion.
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
object ImplicitRecursion { | |
abstract class Nat | |
class Zero extends Nat | |
class Succ[T <: Nat] extends Nat | |
type _0 = Zero | |
type _1 = Succ[_0] | |
type _2 = Succ[_1] | |
type _3 = Succ[_2] | |
type _4 = Succ[_3] | |
type _5 = Succ[_4] | |
type _6 = Succ[_5] | |
type _7 = Succ[_6] | |
type _8 = Succ[_7] | |
type _9 = Succ[_8] | |
implicit def SuccToN[T <: Nat](value: Succ[T])(implicit rec: T => Int): Int = rec(null.asInstanceOf[T]) + 1 | |
implicit def ZeroTo0[T <: Nat](zero: T): Int = 0 | |
def nat2Int[T <: Nat](implicit tr: T => Int): Int = tr(null.asInstanceOf[T]) | |
} | |
import ImplicitRecursion._ | |
println(nat2Int[_8]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment