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.
Scala is a statically typed programming language. This means the compiler determines the type of a variable at compile time. Type declaration is a Scala feature that enables us to declare our own types.
A constructor is a special type of function with no return type. Name of constructor should be same as the name of the class. We define a method inside the class and constructor is also defined inside a class. A constructor is called automatically when we create an object of a class.
Language. Methods in Scala can be parameterized by type as well as value. The syntax is similar to that of generic classes. Type parameters are enclosed in square brackets, while value parameters are enclosed in parentheses.
The primary constructor of a Scala class is a combination of: The constructor parameters. Methods that are called in the body of the class. Statements and expressions that are executed in the body of the class.
In a scala val
assignment like the above:
val name: Tpe = expression
Tpe
is the type of the identifier name
. Hence:
val x: List.type = List
The type of the List
object (aka List module or List companion) is List.type
. This indicates that it is a singleton. All of this is completely orthogonal to the concept of a type constructor in type theory.
In type theory, List
(note: not the companion) is both a type (denoted *
) and a type constructor (denoted * -> *
) because when you supply a type argument to List
(e.g. Int) then you have a type (i.e. List[Int]
). Scala provides syntax for this construction. For example:
def foo[F[_]]: F[Int]
^^^^ ^^^
| + supply a type argument to F[_]
|
+ the type constructor F[_]
But you cannot really implement such a method unless you know more about F
. For example, if there is a implicit scalaz.Monad[F]
, you might use the Monad.pure
value to construct your F[Int]
like so:
def foo[F[_]: Monad]: F[Int] = Monad[F].pure(1)
Scala does let you pass around List
as a type constructor as a type parameter to such a method. For example:
scala> def foo[F[_]]: F[Int] = ???
foo: [F[_]]=> F[Int]
scala> lazy val x = foo[List]
x: List[Int] = <lazy>
However the List
you are supplying in foo[List]
is not the companion
Note the declaration of foo
will result in the following warning:
<console>:11: warning: higher-kinded type should be enabled
by making the implicit value scala.language.higherKinds visible.
This can be achieved by adding the import clause 'import
scala.language.higherKinds'
or by setting the compiler option -language:higherKinds.
See the Scaladoc for value scala.language.higherKinds for a discussion
why the feature should be explicitly enabled.
def foo[F[_]]: F[Int] = ???
^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With