-
Notifications
You must be signed in to change notification settings - Fork 543
Update section on "existential type" to "opaque type" #402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
varkor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type Foo; | ||
fn foo() -> Self::Foo; | ||
} | ||
|
||
struct Quux; | ||
|
||
impl Baz for Quux { | ||
type Foo = impl Bar; | ||
fn foo() -> Self::Foo { ... } | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be
struct Foo;
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it should! Feel free to open a PR with a fix :)Edit: I misread. See comment below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then the whole example should be
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I misread earlier. Really, this code snippet should be read in the context of the previous one. But that's confusing, so maybe we should add the definition of
Foo
here too. So the complete snippet would be: