Last active
October 11, 2016 03:54
-
-
Save jpthompson23/c5daa4b88c23e524f2284b9f019db0ca to your computer and use it in GitHub Desktop.
Scala Generics Worksheet
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 scalaPractice { | |
def squareAddOne[T](x: T) | |
(implicit numeric: Numeric[T]): T = { | |
import numeric._ | |
x*x + one | |
} | |
squareAddOne(3) | |
def squareAddOne2[T: Numeric](x: T): T = { | |
val numeric = implicitly[Numeric[T]] | |
import numeric._ | |
x*x + fromInt(1) | |
} | |
squareAddOne2(4) | |
def squareAddThirty[T: Numeric](x: T): T = { | |
import Numeric.Implicits._ // allows us to use * on type T | |
val numeric = implicitly[Numeric[T]] // gives us numeric.fromInt | |
x*x + numeric.fromInt(30) | |
} | |
squareAddThirty(5) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment