Skip to content

Instantly share code, notes, and snippets.

@diegopacheco
Created July 4, 2018 20:40
Scala Cats Pattern
object InpiredByCats extends App {
// Type Class = Type system functionality
trait Show[T]{
def show(value:T):String
}
// Type Instance = All Instances for Supported Types
object ShowInstances{
implicit val intInstance = new Show[Int] {
override def show(value:Int):String = value.toString()
}
implicit val StringInstance = new Show[String] {
override def show(value:String):String = s"-- ${value} --"
}
}
// Type Interface - Functions for the Type Class instances
object ShowInterface{
def printWithShow[T](value:T)(implicit show:Show[T]):Unit = println( show.show(value) )
}
// Beatiful Syntax Support
implicit class ShowSyntax[T](value:T){
def printWithShow(implicit show:Show[T]):Unit = println(show.show(value))
}
import ShowInstances._
ShowInterface.printWithShow("There we Go")
"Diego".printWithShow
42.printWithShow
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment