Skip to content

Commit 7e9f313

Browse files
committed
Add explanation to pseudocode
1 parent 2599e2e commit 7e9f313

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

docs/docs/reference/intersection-types-spec.md

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ title: "Intersection Types - More Details"
77

88
Syntactically, an intersection type `S & T` is similar to an infix type, where
99
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 `&`.
1212

1313
```
1414
Type ::= ...| InfixType
@@ -18,20 +18,29 @@ InfixType ::= RefinedType {id [nl] RefinedType}
1818
## Subtyping Rules
1919

2020
```
21-
T <: A T <: B
22-
----------------
23-
T <: A & B
21+
T <: A T <: B
22+
----------------
23+
T <: A & B
2424
25-
A <: T
26-
----------------
27-
A & B <: T
25+
A <: T
26+
----------------
27+
A & B <: T
2828
29-
B <: T
30-
----------------
31-
A & B <: T
29+
B <: T
30+
----------------
31+
A & B <: T
3232
```
3333

3434
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+
3544
In another word, `A & B` is the same type as `B & A`, in that sense that the two types
3645
have the same values and are subtypes of each other.
3746

@@ -74,16 +83,19 @@ below in pseudocode:
7483
```
7584
|S & T| = glb(|S|, |T|)
7685
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
8594
```
8695

96+
In the above, we use `|T|` to mean the erased type of `T`, `JArray` means
97+
the type of Java Array.
98+
8799
## Relationship with Compound Type (`with`)
88100

89101
Intersection types `A & B` replace compound types `A with B` in Scala 2. For the

0 commit comments

Comments
 (0)