mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 19:50:14 +01:00
Generics: Set associated constants using where constraints (#1013)
Change the syntax for setting the associated constants and types in an interface implementation for a type from using `let` declarations as in:
```
class Vector(T:! Type) {
impl as Iterable {
let ElementType:! Type = T;
...
}
}
```
to using `where` clauses as in:
```
class Vector(T:! Type) {
impl as Iterable where .ElementType = T {
...
}
}
```
This is an attempt to simplify by removing redundancy, improve consistency by removing a use of `let` that was different than other examples, and better support forward declaration that a type implements an interface while retaining the information needed for type checking.
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
committed by
GitHub
co-authored by
Richard Smith
parent
94265befee
commit
2d567f5824
@@ -197,8 +197,8 @@ class Song {
|
||||
// ...
|
||||
|
||||
// Implementing `Printable` for `Song` inside the definition of `Song`
|
||||
// means all names of `Printable`, such as `F`, are included as a part
|
||||
// of the `Song` API.
|
||||
// without the keyword `external` means all names of `Printable`, such
|
||||
// as `F`, are included as a part of the `Song` API.
|
||||
impl as Printable {
|
||||
// Could use `Self` in place of `Song` here.
|
||||
fn Print[me: Song]() { ... }
|
||||
@@ -594,6 +594,15 @@ Constraints limit the types that the generic function can operate on, but
|
||||
increase the knowledge that may be used in the body of the function to operate
|
||||
on values of those types.
|
||||
|
||||
Constraints are also used when implementing an interface to specify the values
|
||||
of associated types (and other associated constants).
|
||||
|
||||
```
|
||||
class Vector(T:! Movable) {
|
||||
impl as Stack where .ElementType = T { ... }
|
||||
}
|
||||
```
|
||||
|
||||
### Parameterized impls
|
||||
|
||||
Implementations can be parameterized to apply to multiple types. Those
|
||||
@@ -634,3 +643,4 @@ priority order in a prioritization block.
|
||||
- [#818: Constraints for generics (generics details 3)](https://github.com/carbon-language/carbon-lang/pull/818)
|
||||
- [#920: Generic parameterized impls (details 5)](https://github.com/carbon-language/carbon-lang/pull/920)
|
||||
- [#950: Generic details 6: remove facets](https://github.com/carbon-language/carbon-lang/pull/950)
|
||||
- [#1013: Generics: Set associated constants using `where` constraints](https://github.com/carbon-language/carbon-lang/pull/1013)
|
||||
|
||||
Reference in New Issue
Block a user