Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created May 9, 2010 16:43
Show Gist options
  • Save kmizu/395263 to your computer and use it in GitHub Desktop.
Save kmizu/395263 to your computer and use it in GitHub Desktop.
// This file must be compiled under Scala 2.8.0 with -Xexperimental
case class Hoge()
case class Foo()
case class Dummy()
trait C[A] {
type Conv
def method(a: A): Conv
}
trait Show[A] { def format(a: A): String }
object InstanceDeclarations {
implicit object HogeInstance extends C[Hoge] {
type Conv = String
def method(a: Hoge): String = "Hoge"
}
implicit object FooInstance extends C[Foo] {
type Conv = Dummy
def method(a: Foo): Dummy = Dummy()
}
implicit object ShowString extends Show[String] {
def format(a: String): String = a
}
}
import InstanceDeclarations._
object ConstraintsOnAssociativeType2 {
class Workaround[S](s: S) {
def apply(implicit cs: C[S]) = new {
def apply(implicit c: Show[cs.Conv]) = c.format(cs.method(s))
}
}
def main(args: Array[String]) {
println(new Workaround(Hoge()).apply.apply) // OK
//println(new Workaround(Foo()).apply.apply) // Error
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment