Skip to content

Instantly share code, notes, and snippets.

@Moverr
Last active June 1, 2022 10:13
Show Gist options
  • Save Moverr/e1e5d20abe94310b645f867ddaec7a80 to your computer and use it in GitHub Desktop.
Save Moverr/e1e5d20abe94310b645f867ddaec7a80 to your computer and use it in GitHub Desktop.
object CalcExample extends App{
trait calculate[T]{
def calc(a:T,b:T)(c:String):T
}
implicit object CalcIntegers extends calculate[Int]{
override def calc(a:Int,b:Int)(c:String):Int= c match {
case "+" => a +b
case "-" => a-b
case "/" => a/b
case "*" => a*b
case _ => 0
}
}
implicit object CalcDoubles extends calculate[Double] {
override def calc(a:Double,b:Double)(c:String):Double= c match {
case "+" => a +b
case "-" => a-b
case "/" => a/b
case "*" => a*b
case _ => 0
}
}
object calculate {
def calc[T](a:T,b:T)(c:String)(implicit calca:calculate[T]):T={
calca.calc(a,b)(c)
}
}
//example
println(calculate.calc(10,2)("/"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment