For an introduction to type parameterization, we'll refer to two examples we've already seen to try to make some sense of it. I know you're following the chapter with interest and you've gone through the examples and concepts we've talked about, so let's do an exercise. Think of our savior, Option[T]
type and try to think why you would want to pass a type (as it requires T
to be a type) to Option
. What purpose can it serve?
I think you've come up with some idea. Maybe you thought that by passing a type of our choice, we can make our code with the Option
type work in more than one scenario. If you thought so, great! Let's call it generalizing our solution. And moreover, let's call the approach a generic approach to programming. How does it look? Let's take a look at the following code:
object TypeParameterization { def main(args: Array[String]): Unit = { val mayBeAnInteger = Some("101") val mayBeADouble = Some("101.0") val mayBeTrue = Some("true...