From 5822f486818172e502485cc0e9e171aeff72c089 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 1 Aug 2019 00:44:51 +0100 Subject: [PATCH] Update section on "existential type" to "opaque type" --- src/existential-types.md | 48 ------------------ src/opaque-types-type-alias-impl-trait.md | 62 +++++++++++++++++++++++ 2 files changed, 62 insertions(+), 48 deletions(-) delete mode 100644 src/existential-types.md create mode 100644 src/opaque-types-type-alias-impl-trait.md diff --git a/src/existential-types.md b/src/existential-types.md deleted file mode 100644 index ef20167c1..000000000 --- a/src/existential-types.md +++ /dev/null @@ -1,48 +0,0 @@ -# Existential Types - -Existential types are essentially strong type aliases which only expose -a specific set of traits as their interface and the concrete type in the -background is inferred from a certain set of use sites of the existential -type. - -In the language they are expressed via - -```rust,ignore -existential type Foo: Bar; -``` - -This is in existential type named `Foo` which can be interacted with via -the `Bar` trait's interface. - -Since there needs to be a concrete background type, you can currently -express that type by using the existential type in a "defining use site". - -```rust,ignore -struct Struct; -impl Bar for Struct { /* stuff */ } -fn foo() -> Foo { - Struct -} -``` - -Any other "defining use site" needs to produce the exact same type. - -## Defining use site(s) - -Currently only the return value of a function inside can -be a defining use site of an existential type (and only if the return -type of that function contains the existential type). - -The defining use of an existential type can be any code *within* the parent -of the existential type definition. This includes any siblings of the -existential type and all children of the siblings. - -The initiative for *"not causing fatal brain damage to developers due to -accidentally running infinite loops in their brain while trying to -comprehend what the type system is doing"* has decided to disallow children -of existential types to be defining use sites. - -### Associated existential types - -Associated existential types can be defined by any other associated item -on the same trait `impl` or a child of these associated items. diff --git a/src/opaque-types-type-alias-impl-trait.md b/src/opaque-types-type-alias-impl-trait.md new file mode 100644 index 000000000..e78d3c75d --- /dev/null +++ b/src/opaque-types-type-alias-impl-trait.md @@ -0,0 +1,62 @@ +# Opaque types (type alias `impl Trait`) + +Opaque types are syntax to declare an opaque type alias that only +exposes a specific set of traits as their interface; the concrete type in the +background is inferred from a certain set of use sites of the opaque type. + +This is expressed by using `impl Trait` within type aliases, for example: + +```rust,ignore +type Foo = impl Bar; +``` + +This declares an opaque type named `Foo`, of which the only information is that +it implements `Bar`. Therefore, any of `Bar`'s interface can be used on a `Foo`, +but nothing else (regardless of whether it implements any other traits). + +Since there needs to be a concrete background type, you can currently +express that type by using the opaque type in a "defining use site". + +```rust,ignore +struct Struct; +impl Bar for Struct { /* stuff */ } +fn foo() -> Foo { + Struct +} +``` + +Any other "defining use site" needs to produce the exact same type. + +## Defining use site(s) + +Currently only the return value of a function can be a defining use site +of an opaque type (and only if the return type of that function contains +the opaque type). + +The defining use of an opaque type can be any code *within* the parent +of the opaque type definition. This includes any siblings of the +opaque type and all children of the siblings. + +The initiative for *"not causing fatal brain damage to developers due to +accidentally running infinite loops in their brain while trying to +comprehend what the type system is doing"* has decided to disallow children +of opaque types to be defining use sites. + +### Associated opaque types + +Associated opaque types can be defined by any other associated item +on the same trait `impl` or a child of these associated items. For instance: + +```rust,ignore +trait Baz { + type Foo; + fn foo() -> Self::Foo; +} + +struct Quux; + +impl Baz for Quux { + type Foo = impl Bar; + fn foo() -> Self::Foo { ... } +} +```