Skip to content

Instantly share code, notes, and snippets.

@ShaneDelmore
Created June 20, 2018 00:05
Show Gist options
  • Save ShaneDelmore/ea30500d690fac873885d521a9db256c to your computer and use it in GitHub Desktop.
Save ShaneDelmore/ea30500d690fac873885d521a9db256c to your computer and use it in GitHub Desktop.
Typeclass example
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