MOCKSTACKS
EN
Questions And Answers

More Tutorials









Scala Type Parameterization

Examples

The Option type


A nice example of a parameterized type is the Option type. It is essentially just the following definition (with several more methods defined on the type):

sealed abstract class Option[+A] {
 def isEmpty: Boolean
 def get: A
 final def fold[B](ifEmpty: => B)(f: A => B): B =
 if (isEmpty) ifEmpty else f(this.get)
 // lots of methods...
}
case class Some[A](value: A) extends Option[A] {
 def isEmpty = false
 def get = value
}
case object None extends Option[Nothing] {
 def isEmpty = true
 def get = throw new NoSuchElementException("None.get")
}

We can also see that this has a parameterized method, fold, which returns something of type B.

Parameterized Methods


The return type of a method can depend on the type of the parameter. In this example, x is the parameter, A is the type of x, which is known as the type parameter.

def f[A](x: A): A = x
f(1) // 1
f("two") // "two"
f[Float](3) // 3.0F

Scala will use type inference to determine the return type, which constrains what methods may be called on the parameter. Thus, care must be taken: the following is a compile-time error because * is not defined for every type A:

def g[A](x: A): A = 2 * x // Won't compile

Generic collection


Defining the list of Ints


trait IntList { ... }
class Cons(val head: Int, val tail: IntList) extends IntList { ... }
class Nil extends IntList { ... }

but what if we need to define the list of Boolean, Double etc?

Defining generic list


trait List[T] {
 def isEmpty: Boolean
 def head: T
 def tail: List[T]
}
class Cons[T](val head: [T], val tail: List[T]) extends List[T] {
 def isEmpty: Boolean = false
}
class Nil[T] extends List[T] {
 def isEmpty: Boolean = true
 def head: Nothing = throw NoSuchElementException("Nil.head")
 def tail: Nothing = throw NoSuchElementException("Nil.tail")
}


Conclusion

In this page (written and validated by ) you learned about Scala Type Parameterization . What's Next? If you are interested in completing Scala tutorial, your next topic will be learning about: Scala Type Variance.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.