Last active
June 1, 2022 10:13
-
-
Save Moverr/e1e5d20abe94310b645f867ddaec7a80 to your computer and use it in GitHub Desktop.
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
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