What is type and what is type constructor in scala

Viewed 6388

I'm a bit confused about understanding of what type in scala means.

In documents I read that List[Int] is a type and List is a type constructor.

but what keyword type means when I write the following?

val ls: scala.collection.immutable.List.type = scala.collection.immutable.List

And how this type related to type that can be defined as a field in a trait for example.

3 Answers

In type-level programming you’d think of a List[+A] being a type constructor:

That is:

-List[+A] takes a type parameter (A),

-by itself it’s not a valid type, you need to fill in the A somehow - "construct the type",

-by filling it in with String you’d get List[String] which is a concrete type.

In Scala it is not valid to say something is of type List. Scala is more strict here, and won’t allow us to use just a List in the place of a type, as it’s expecting a real type - not a type constructor.

In brief: List[+A] is a type constructor whereas List[String] is a real type.

Related