Created
June 20, 2018 00:05
-
-
Save ShaneDelmore/ea30500d690fac873885d521a9db256c to your computer and use it in GitHub Desktop.
Typeclass 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
trait Longable{ def toLong(): Long } | |
implicit class IntCanLong(int: Int) extends Longable { | |
def toLong(): Long = int.toLong | |
} | |
implicit class StringCanLong(str: String) extends Longable { | |
def toLong(): Long = str.toInt.toLong | |
} | |
val l: List[Longable] = List(1,"5") | |
val myMap = Map[String, Longable]( | |
"one" -> 1, | |
"two" -> "2" | |
) | |
myMap.valuesIterator.foreach(l => println(l.toLong)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment