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 2, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment