Skip to content

Commit 9efa9f1

Browse files
oli-obkmark-i-m
authored andcommitted
Explain existential types
1 parent 1f8e117 commit 9efa9f1

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
- [Type checking](./type-checking.md)
4646
- [Method Lookup](./method-lookup.md)
4747
- [Variance](./variance.md)
48+
- [Existential Types](./existential-types.md)
4849
- [The MIR (Mid-level IR)](./mir/index.md)
4950
- [MIR construction](./mir/construction.md)
5051
- [MIR visitor and traversal](./mir/visitor.md)

src/existential-types.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Existential Types
2+
3+
Existential types are essentially strong type aliases which only expose
4+
a specific set of traits as their interface and the concrete type in the
5+
background is inferred from a certain set of use sites of the existential
6+
type.
7+
8+
In the language they are expressed via
9+
10+
```rust
11+
existential type Foo: Bar;
12+
```
13+
14+
This is in existential type named `Foo` which can be interacted with via
15+
the `Bar` trait's interface.
16+
17+
Since there needs to be a concrete background type, you can currently
18+
express that type by using the existential type in a "defining use site".
19+
20+
```rust
21+
struct Struct;
22+
impl Bar for Struct { /* stuff */ }
23+
fn foo() -> Foo {
24+
Struct
25+
}
26+
```
27+
28+
Any other "defining use site" needs to produce the exact same type.
29+
30+
## Defining use site(s)
31+
32+
Currently only the return value of a function inside can
33+
be a defining use site of an existential type (and only if the return
34+
type of that function contains the existential type).
35+
36+
The defining use of an existential type can be any code *within* the parent
37+
of the existential type definition. This includes any siblings of the
38+
existential type and all children of the siblings.
39+
40+
The initiative for *"not causing fatal brain damage to developers due to
41+
accidentally running infinite loops in their brain while trying to
42+
comprehend what the type system is doing"* has decided to disallow children
43+
of existential types to be defining use sites.
44+
45+
### Associated existential types
46+
47+
Associated existential types can be defined by any other associated item
48+
on the same trait `impl` or a child of these associated items.

0 commit comments

Comments
 (0)