def myGenericAdditionFun[T](x: T, y: T): Int =
// Casting for x
val myMatchX = x match
case x: Int => x
case x: Double => x.toInt
case x: String => x.toInt
case x: Char => x.asDigit
case x: Boolean => if (x) 1 else 0
// Casting for x
val myMatchY = y match
case y: Int => y
case y: Double => y.toInt
case y: String => y.toInt
case y: Char => y.asDigit
case y: Boolean => if (y) 1 else 0
// Return addition
myMatchX + myMatchY
myGenericAdditionFun(1, 2) // res0: Int = 3
myGenericAdditionFun(1.1, 2.2) // res1: Int = 3
myGenericAdditionFun('1', '2') // res2: Int = 3
myGenericAdditionFun("1", "2") // res3: Int = 3
myGenericAdditionFun(true, false) // res4: Int = 1
myGenericAdditionFun(true, true) // res5: Int = 2
myGenericAdditionFun(false, false) // res6: Int = 0
myGenericAdditionFun(1, '2') // res7: Int = 3
myGenericAdditionFun("1", '2') // res8: Int = 3
myGenericAdditionFun(1.4, '2') // res9: Int = 3
Scala- 15 min
- Blog
- Computer Science
- Julia
Julia is a reasonably new, open-source, high-level, dynamically-typed programming language. It’s a multi-platform language supported on Linux, macOS, Windows and FreeBSD….