|
| 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