Type Parameters and Generics
Generic Box Value
Store one value in a class with a type parameter.
Generic Box Value
GenericBoxValue.scala
case class Box[A](value: A)
object Main {
def main(args: Array[String]): Unit = {
val points =
val box = Box[Int](points)
val doubled = box.value * 2
println("value=" + box.value)
println("doubled=" + doubled)
}
}
case class Box[A](value: A)
object Main {
def main(args: Array[String]): Unit = {
val points =
val box = Box[Int](points)
val doubled = box.value * 2
println("value=" + box.value)
println("doubled=" + doubled)
}
}
case class Box[A](value: A)
object Main {
def main(args: Array[String]): Unit = {
val points =
val box = Box[Int](points)
val doubled = box.value * 2
println("value=" + box.value)
println("doubled=" + doubled)
}
}
generic-class
A class can take a type parameter too. `Box[A]` stores one value while keeping the value's type known to the compiler.