Created
July 3, 2012 10:50
-
-
Save tonymorris/3039030 to your computer and use it in GitHub Desktop.
How can I get this program to terminate? (non-strict implicit not permitted)
This file contains 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
case class Swizzler[A](swizzle: String => A) { | |
def map[B](f: A => B): Swizzler[B] = | |
Swizzler(f compose swizzle) | |
def flatMap[B](f: A => Swizzler[B]): Swizzler[B] = | |
Swizzler(s => f(swizzle(s)).swizzle(s)) | |
} | |
object Swizzler { | |
implicit def ListSwizzler[A: Swizzler]: Swizzler[List[A]] = | |
Swizzler[List[A]](_.toList map (c => implicitly[Swizzler[A]].swizzle(c.toString))) | |
implicit def Tuple2Swizzler[A: Swizzler, B: Swizzler]: Swizzler[(A, B)] = | |
for { | |
aa <- implicitly[Swizzler[A]] | |
bb <- implicitly[Swizzler[B]] | |
} yield (aa, bb) | |
implicit val StringSwizzler: Swizzler[String] = | |
Swizzler(s => s) | |
} | |
case class Person(name: String, children: List[Person]) | |
object Person { | |
implicit def PersonSwizzler: Swizzler[Person] = | |
implicitly[Swizzler[(String, List[Person])]] map { case (n, c) => Person(n, c) } | |
} | |
object Main { | |
def main(args: Array[String]) { | |
val pswizzle = implicitly[Swizzler[Person]] | |
println(pswizzle swizzle "abc") | |
} | |
} | |
/* | |
$ scalac Swizzler.scala && scala Main | |
java.lang.StackOverflowError | |
at Person$.PersonSwizzler(Swizzler.scala:29) | |
at Person$.PersonSwizzler(Swizzler.scala:29) | |
at Person$.PersonSwizzler(Swizzler.scala:29) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment