O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Parameterized Types

As we’ve seen, not only is extending a class concise in Scala, but it’s also streamlined to alleviate some common programming errors. Generic or parameterized types further help to create classes and functions that can work with multiple different types. The types can be specified at compile time instead of code writing time, making the code more extensible without losing type safety.

Since in Scala you can create stand-alone functions, you can create parameterized functions as well. Let’s create one:

WorkingWithObjects/Parameterized.scala
 
def​ echo[T](input1: T, input2: T) =
 
println(s​"got $input1 (${input1.getClass}) $input2 (${input2.getClass})"​)

Rather than specifying the echo function’s parameter types to be some ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required