Created
April 23, 2016 09:03
-
-
Save bkase/3d9c93fc01cedebeaf1b808f5414651a to your computer and use it in GitHub Desktop.
Playing with typeclasses in Scala
This file contains hidden or 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
import simulacrum._ | |
@typeclass trait CanTruthy[A] { self => | |
@op("!!") def truthy(a: A): Boolean | |
} | |
object Main extends App { | |
import CanTruthy.ops._ | |
implicit val truthyInt = | |
new CanTruthy[Int] { | |
def truthy(a: Int) = a != 0 | |
} | |
implicit val truthyString = | |
new CanTruthy[String] { | |
def truthy(a: String) = a != "" | |
} | |
println(5 !!) | |
println("hello" !!) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment