Skip to content

Commit d9dfc21

Browse files
committed
Apply kind-polymorphism.md.
1 parent 645e149 commit d9dfc21

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

docs/_spec/03-types.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ chapter: 3
2626
```
2727

2828
We distinguish between proper types and type constructors, which take type parameters and yield types.
29+
All types have a _kind_, either the kind of proper types or a _higher kind_.
2930
A subset of proper types called _value types_ represents sets of (first-class) values.
30-
Value types are either _concrete_ or _abstract_.
31+
Types are either _concrete_ or _abstract_.
3132

3233
Every concrete value type can be represented as a _class type_, i.e. a [type designator](#type-designators) that refers to a [class or a trait](05-classes-and-objects.html#class-definitions) [^1], or as a [compound type](#compound-types) representing an intersection of types, possibly with a [refinement](#compound-types) that further constrains the types of its members.
3334

3435
<!--
3536
A shorthand exists for denoting [function types](#function-types)
3637
-->
37-
Abstract value types are introduced by [type parameters](04-basic-declarations-and-definitions.html#type-parameters) and [abstract type bindings](04-basic-declarations-and-definitions.html#type-declarations-and-type-aliases).
38+
Abstract types are introduced by [type parameters](04-basic-declarations-and-definitions.html#type-parameters) and [abstract type bindings](04-basic-declarations-and-definitions.html#type-declarations-and-type-aliases).
3839
Parentheses in types can be used for grouping.
3940

4041
[^1]: We assume that objects and packages also implicitly
@@ -49,6 +50,10 @@ Non-value types are expressed indirectly in Scala.
4950
E.g., a method type is described by writing down a method signature, which in itself is not a real type, although it gives rise to a corresponding [method type](#method-types).
5051
Type constructors are another example, as one can write `type Swap[m[_, _], a,b] = m[b, a]`, but there is no syntax to write the corresponding anonymous type function directly.
5152

53+
`AnyKind` is the super type of all types in the Scala type system.
54+
It has all possible kinds to encode [kind polymorphism](#kind-polymorphism).
55+
As such, it is neither a value type nor a type constructor.
56+
5257
## Paths
5358

5459
```ebnf
@@ -502,6 +507,42 @@ val f = 0
502507
define a function `f} which has type `(x: T)T ´\overload´ Int`.
503508
-->
504509

510+
## Kind Polymorphism
511+
512+
Type parameters are normally partitioned into _kinds_, indicated by the top type of which it is a subtype.
513+
Proper types are the types of values and are subtypes of `Any`.
514+
Higher-kinded types are type constructors such as `List` or `Map`.
515+
Covariant single argument type constructors such as `List` are subtypes of `[+X] =>> Any`.
516+
The `Map` type constructor is a subtype of `[X, +Y] =>> Any`.
517+
518+
A type can be used only as prescribed by its kind.
519+
Subtypes of `Any` cannot be applied to type arguments whereas subtypes of `[X] =>> Any` _must_ be applied to a type argument, unless they are passed to type parameters of the same kind.
520+
521+
A type parameter whose upper bound is [`scala.AnyKind`](https://scala-lang.org/api/3.x/scala/AnyKind.html) can have any kind and is called an _any-kinded type_.
522+
523+
```scala
524+
def f[T <: AnyKind] = ...
525+
```
526+
527+
The actual type arguments of `f` can then be types of arbitrary kinds.
528+
So the following are all legal:
529+
530+
```scala
531+
f[Int]
532+
f[List]
533+
f[Map]
534+
f[[X] =>> String]
535+
```
536+
537+
Since the actual kind of an any-kinded type is unknown, its usage is heavily restricted.
538+
An any-kinded type can neither be the type of a value, nor be instantiated with type parameters.
539+
The only thing one can do with an any-kinded type is to pass it to another any-kinded type argument.
540+
541+
`AnyKind` plays a special role in Scala's subtype system.
542+
It is a supertype of all other types, no matter what their kind is.
543+
It is also assumed to be kind-compatible with all other types.
544+
Furthermore, `AnyKind` is itself an any-kinded type, so it cannot be the type of values and it cannot be instantiated.
545+
505546
## Base Types and Member Definitions
506547

507548
Types of class members depend on the way the members are referenced.
@@ -588,8 +629,9 @@ Equivalence ´(\equiv)´ between types is the smallest congruence [^congruence]
588629
The conformance relation ´(<:)´ is the smallest transitive relation that satisfies the following conditions.
589630

590631
- Conformance includes equivalence. If ´T \equiv U´ then ´T <: U´.
591-
- For every value type `T`, `scala.Nothing <: ´T´ <: scala.Any`.
592-
- For every type constructor ´T´ (with any number of type parameters), `scala.Nothing <: ´T´ <: scala.Any`.
632+
- For every type `´T´` (of any kind), `scala.Nothing <: ´T´ <: scala.AnyKind`.
633+
- For every value type `´T´`, `´T´ <: scala.Any`.
634+
- For every type constructor `´T´` with type parameters `[´U_1´, ..., ´U_n´]`, `[´U_1´, ..., ´U_n´] =>> scala.Nothing <: ´T´ <: [´U_1´, ..., ´U_n´] =>> scala.Any`.
593635
- For every value type ´T´, `scala.Null <: ´T´` unless `´T´ <: scala.AnyVal`.
594636
- A type variable or abstract type ´t´ conforms to its upper bound and its lower bound conforms to ´t´.
595637
- A class type or parameterized type conforms to any of its base-types.
@@ -716,6 +758,7 @@ _Type erasure_ is a mapping from (possibly generic) types to non-generic types.
716758
We write ´|T|´ for the erasure of type ´T´.
717759
The erasure mapping is defined as follows.
718760

761+
- The erasure of `scala.AnyKind` is `Object`.
719762
- The erasure of an alias type is the erasure of its right-hand side.
720763
- The erasure of an abstract type is the erasure of its upper bound.
721764
- The erasure of the parameterized type `scala.Array´[T_1]´` is `scala.Array´[|T_1|]´`.

docs/_spec/TODOreference/other-new-features/kind-polymorphism.md renamed to docs/_spec/APPLIEDreference/other-new-features/kind-polymorphism.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,4 @@ through advanced uses of implicits.
3838

3939
(todo: insert good concise example)
4040

41-
Some technical details: [`AnyKind`](https://scala-lang.org/api/3.x/scala/AnyKind.html) is a synthesized class just like `Any`, but without any members. It extends no other class.
42-
It is declared `abstract` and `final`, so it can be neither instantiated nor extended.
43-
4441
`AnyKind` plays a special role in Scala's subtype system: It is a supertype of all other types no matter what their kind is. It is also assumed to be kind-compatible with all other types. Furthermore, `AnyKind` is treated as a higher-kinded type (so it cannot be used as a type of values), but at the same time it has no type parameters (so it cannot be instantiated).
45-
46-
**Note**: This feature is considered experimental but stable and it can be disabled under compiler flag
47-
(i.e. `-Yno-kind-polymorphism`).

0 commit comments

Comments
 (0)