case classes that help use create new type of objects. For instance List
is a type constructory.
Generic types that take parameters such as —
trait Foo[T[_]] {
def bar(x: T[Int]): Unit
}
When we define an algebraic data type using sealed traits we allow the compiler to perform exhaustiveness checking. In simpler words, this means the compiler will shout at us if we miss out a case in our structural recursion.
scala> Option(1) match {
| case None => "Yeah"
| }
<console>:8: warning: match may not be exhaustive.
It would fail on the following input: Some(_)
Option(1) match {
^
A sealed trait can only be extended within the file in which it defined. more
final class/trait cannot be extended anywhere. more
A type constructor is a universally quantified type, which can be used to construct types.
sealed trait List[A]
case class Nil[A]() extends List[A]
case class Cons[A](head: A, tail: List[A]) extends List[A]
List
is type constructor, which defines a family of List-like types, including List[Boolean]
(the type of lists of booleans).
List
is said to be universally quantified over its type variable A
.
- update() used as the default setter method - apply() used as the default getter method
- this() used as a constructor