Skip to content

Instantly share code, notes, and snippets.

@jpthompson23
Last active October 11, 2016 03:54
Show Gist options
  • Save jpthompson23/c5daa4b88c23e524f2284b9f019db0ca to your computer and use it in GitHub Desktop.
Save jpthompson23/c5daa4b88c23e524f2284b9f019db0ca to your computer and use it in GitHub Desktop.
Scala Generics Worksheet
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