File tree Expand file tree Collapse file tree 2 files changed +5
-10
lines changed
docs/docs/reference/new-types Expand file tree Collapse file tree 2 files changed +5
-10
lines changed Original file line number Diff line number Diff line change @@ -88,7 +88,7 @@ treatment of singleton types which are also widened to their underlying type
88
88
unless explicitly specified. and the motivation is the same: inferring types
89
89
which are "too precise" can lead to unintuitive typechecking issues later on.
90
90
91
- Note: Since this behavior severely limits the usability of union types, it might
91
+ Note: Since this behavior limits the usability of union types, it might
92
92
be changed in the future. For example by not widening unions that have been
93
93
explicitly written down by the user and not inferred, or by not widening a type
94
94
argument when the corresponding type parameter is covariant. See
@@ -149,7 +149,3 @@ the erased lub is computed as follows:
149
149
The reason to pick last is that we prefer classes over traits that way,
150
150
which leads to more predictable bytecode and (?) faster dynamic dispatch.
151
151
152
- ## Limitations
153
-
154
- In a union type ` A | B ` , neither ` A ` nor ` B ` is allowed to be a singleton type.
155
- This is an implementation restriction that may be lifted in the future.
Original file line number Diff line number Diff line change @@ -3,7 +3,8 @@ layout: doc-page
3
3
title : " Union Types"
4
4
---
5
5
6
- Used on types, the ` | ` operator creates a union type.
6
+ A union type ` A | B ` has as values all values of type ` A ` and also all values of type ` B ` .
7
+
7
8
8
9
``` scala
9
10
case class UserName (name : String ) {
@@ -22,13 +23,11 @@ def help(id: UserName | Password) = {
22
23
}
23
24
```
24
25
25
- Union types are dual of intersection types. Values of type ` A | B ` are
26
- all values of type ` A ` and all values of type ` B ` . ` | ` is _ commutative_ :
26
+ Union types are duals of intersection types. ` | ` is _ commutative_ :
27
27
` A | B ` is the same type as ` B | A ` .
28
28
29
29
The compiler will assign a union type to an expression only if such a
30
- type is explicitly given.
31
- This can be seen in the following REPL transcript:
30
+ type is explicitly given. This can be seen in the following REPL transcript:
32
31
33
32
``` scala
34
33
scala> val password = Password (123 )
You can’t perform that action at this time.
0 commit comments