You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/_spec/03-types.md
+47-4Lines changed: 47 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -26,15 +26,16 @@ chapter: 3
26
26
```
27
27
28
28
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_.
29
30
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_.
31
32
32
33
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.
33
34
34
35
<!--
35
36
A shorthand exists for denoting [function types](#function-types)
36
37
-->
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).
38
39
Parentheses in types can be used for grouping.
39
40
40
41
[^1]: We assume that objects and packages also implicitly
@@ -49,6 +50,10 @@ Non-value types are expressed indirectly in Scala.
49
50
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).
50
51
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.
51
52
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
+
52
57
## Paths
53
58
54
59
```ebnf
@@ -502,6 +507,42 @@ val f = 0
502
507
define a function `f} which has type `(x: T)T ´\overload´ Int`.
503
508
-->
504
509
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
+
deff[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
+
505
546
## Base Types and Member Definitions
506
547
507
548
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]
588
629
The conformance relation ´(<:)´ is the smallest transitive relation that satisfies the following conditions.
589
630
590
631
- 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`.
593
635
- For every value type ´T´, `scala.Null <: ´T´` unless `´T´ <: scala.AnyVal`.
594
636
- A type variable or abstract type ´t´ conforms to its upper bound and its lower bound conforms to ´t´.
595
637
- 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.
716
758
We write ´|T|´ for the erasure of type ´T´.
717
759
The erasure mapping is defined as follows.
718
760
761
+
- The erasure of `scala.AnyKind` is `Object`.
719
762
- The erasure of an alias type is the erasure of its right-hand side.
720
763
- The erasure of an abstract type is the erasure of its upper bound.
721
764
- The erasure of the parameterized type `scala.Array´[T_1]´` is `scala.Array´[|T_1|]´`.
Copy file name to clipboardExpand all lines: docs/_spec/APPLIEDreference/other-new-features/kind-polymorphism.md
-6Lines changed: 0 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -38,10 +38,4 @@ through advanced uses of implicits.
38
38
39
39
(todo: insert good concise example)
40
40
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
-
44
41
`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
0 commit comments