Last active
December 27, 2015 03:29
-
-
Save mikea/7259269 to your computer and use it in GitHub Desktop.
I want C[T] to have different method sets based on T. I thought I could use implicits, but can't make it work. Any idea how to fix this example?
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
class Printer[T](_t: T) { | |
def t = _t | |
def print():Unit = { println(t) } | |
} | |
class IterablePrinter[T, I <: Iterable[T]](p : Printer[I]) { | |
def printEach() = p.t.foreach(println) | |
} | |
object Test extends App { | |
new Printer(100).print() | |
new Printer(List(1, 2, 3)).print() | |
new IterablePrinter[Int, List[Int]](new Printer(List(1, 2, 3))).printEach() | |
// Define implicits to make this work: | |
new Printer(List(1, 2, 3)).printEach() | |
} |
sergey-scherbina
commented
Nov 1, 2013
Guys, you are missing important point: the task is not for print to work differently, but to have an extra method that is present only when I can prove that T is iterable..
object TestApp extends App {
trait Printer[-T] {
def print(p: T)
}
trait EachPrinter[-T] extends Printer[T] {
def printEach(ps: Iterable[T])
}
implicit class PrinterOp[T: Printer](val p: T) {
def print = implicitly[Printer[T]].print(p)
}
implicit class EachPrinterOp[T: EachPrinter](val p: Iterable[T]) {
def printEach = implicitly[EachPrinter[T]].printEach(p)
}
object Printer {
trait AnyPrinter extends Printer[Any] {
def print(p: Any) = println(p)
}
implicit object AnyPrinter extends AnyPrinter
}
object EachPrinter {
trait AnyEachPrinter extends EachPrinter[Any] with Printer.AnyPrinter {
def printEach(p: Iterable[Any]) = p foreach print
}
implicit object AnyEachPrinter extends AnyEachPrinter
}
"hello".print
//"hello".printEach // Doesn't compile
List("hello", "world").print
List("hello", "world").printEach
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment