A Tour of Scala: Polymorphic Methods

Methods in Scala can be parameterized with both values and types. Like on the class level, value parameters are enclosed in a pair of parentheses, while type parameters are declared within a pair of brackets.

Method dup in object PolyTest is parameterized with type T and with the value parameters x: T and n: Int. When method dup is called, the programmer provides the required parameters (see line 5 in the program above), but as line 6 in the program above shows, the programmer is not required to give actual type parameters explicitly. The type system of Scala can infer such types. This is done by looking at the types of the given value parameters and at the context where the method is called.

Please note that the trait Application is designed for writing short test programs, but should be avoided for production code as it may affect the ability of the JVM to optimize the resulting code; please use def main() instead.