@@ -7,8 +7,8 @@ title: "Intersection Types - More Details"
7
7
8
8
Syntactically, an intersection type ` S & T ` is similar to an infix type, where
9
9
the infix operator is ` & ` . ` & ` is treated as a soft keyword. That is, it is a
10
- normal identifier with the usual precedence. But a type of the form A & B is
11
- always recognized as an intersection type, without trying to resolve & .
10
+ normal identifier with the usual precedence. But a type of the form ` A & B ` is
11
+ always recognized as an intersection type, without trying to resolve ` & ` .
12
12
13
13
```
14
14
Type ::= ...| InfixType
@@ -18,20 +18,29 @@ InfixType ::= RefinedType {id [nl] RefinedType}
18
18
## Subtyping Rules
19
19
20
20
```
21
- T <: A T <: B
22
- ----------------
23
- T <: A & B
21
+ T <: A T <: B
22
+ ----------------
23
+ T <: A & B
24
24
25
- A <: T
26
- ----------------
27
- A & B <: T
25
+ A <: T
26
+ ----------------
27
+ A & B <: T
28
28
29
- B <: T
30
- ----------------
31
- A & B <: T
29
+ B <: T
30
+ ----------------
31
+ A & B <: T
32
32
```
33
33
34
34
From the rules above, we can show that ` & ` is _ commutative_ : ` A & B <: B & A ` for any type ` A ` and ` B ` .
35
+
36
+ ```
37
+ B <: B A <: A
38
+ ---------- -----------
39
+ A & B <: B A & B <: A
40
+ ---------------------------
41
+ A & B <: B & A
42
+ ```
43
+
35
44
In another word, ` A & B ` is the same type as ` B & A ` , in that sense that the two types
36
45
have the same values and are subtypes of each other.
37
46
@@ -74,16 +83,19 @@ below in pseudocode:
74
83
```
75
84
|S & T| = glb(|S|, |T|)
76
85
77
- glb(JArray(A), JArray(B)) = JArray(glb(A, B))
78
- glb(JArray(T), _) = JArray(T)
79
- glb(_, JArray(T)) = JArray(T)
80
- glb(A, B) = A if A extends B
81
- glb(A, B) = B if B extends A
82
- glb(A, _) = A if A is not a trait
83
- glb(_, B) = B if B is not a trait
84
- glb(A, _) = A
86
+ glb(JArray(A), JArray(B)) = JArray(glb(A, B))
87
+ glb(JArray(T), _) = JArray(T)
88
+ glb(_, JArray(T)) = JArray(T)
89
+ glb(A, B) = A if A extends B
90
+ glb(A, B) = B if B extends A
91
+ glb(A, _) = A if A is not a trait
92
+ glb(_, B) = B if B is not a trait
93
+ glb(A, _) = A
85
94
```
86
95
96
+ In the above, we use ` |T| ` to mean the erased type of ` T ` , ` JArray ` means
97
+ the type of Java Array.
98
+
87
99
## Relationship with Compound Type (` with ` )
88
100
89
101
Intersection types ` A & B ` replace compound types ` A with B ` in Scala 2. For the
0 commit comments