|
@@ -17,6 +17,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
- [Implementing multiple interfaces](#implementing-multiple-interfaces)
|
|
- [Implementing multiple interfaces](#implementing-multiple-interfaces)
|
|
|
- [External impl](#external-impl)
|
|
- [External impl](#external-impl)
|
|
|
- [Qualified member names](#qualified-member-names)
|
|
- [Qualified member names](#qualified-member-names)
|
|
|
|
|
+ - [Access](#access)
|
|
|
- [Generics](#generics)
|
|
- [Generics](#generics)
|
|
|
- [Implementation model](#implementation-model)
|
|
- [Implementation model](#implementation-model)
|
|
|
- [Interfaces recap](#interfaces-recap)
|
|
- [Interfaces recap](#interfaces-recap)
|
|
@@ -35,6 +36,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
- [Extending adapter](#extending-adapter)
|
|
- [Extending adapter](#extending-adapter)
|
|
|
- [Use case: Using independent libraries together](#use-case-using-independent-libraries-together)
|
|
- [Use case: Using independent libraries together](#use-case-using-independent-libraries-together)
|
|
|
- [Use case: Defining an impl for use by other types](#use-case-defining-an-impl-for-use-by-other-types)
|
|
- [Use case: Defining an impl for use by other types](#use-case-defining-an-impl-for-use-by-other-types)
|
|
|
|
|
+ - [Use case: Private impl](#use-case-private-impl)
|
|
|
- [Adapter with stricter invariants](#adapter-with-stricter-invariants)
|
|
- [Adapter with stricter invariants](#adapter-with-stricter-invariants)
|
|
|
- [Associated constants](#associated-constants)
|
|
- [Associated constants](#associated-constants)
|
|
|
- [Associated class functions](#associated-class-functions)
|
|
- [Associated class functions](#associated-class-functions)
|
|
@@ -591,6 +593,24 @@ p.(Plot.Drawable.Draw)();
|
|
|
C++, adding `ClassName::` in front of a member name to disambiguate, such as
|
|
C++, adding `ClassName::` in front of a member name to disambiguate, such as
|
|
|
[names defined in both a parent and child class](https://stackoverflow.com/questions/357307/how-to-call-a-parent-class-function-from-derived-class-function).
|
|
[names defined in both a parent and child class](https://stackoverflow.com/questions/357307/how-to-call-a-parent-class-function-from-derived-class-function).
|
|
|
|
|
|
|
|
|
|
+### Access
|
|
|
|
|
+
|
|
|
|
|
+An `impl` must be visible to all code that can see both the type and the
|
|
|
|
|
+interface being implemented:
|
|
|
|
|
+
|
|
|
|
|
+- If either the type or interface is private to a single file, then since the
|
|
|
|
|
+ only way to define the `impl` is to use that private name, the `impl` must
|
|
|
|
|
+ be defined private to that file as well.
|
|
|
|
|
+- Otherwise, if the type or interface is private but declared in an API file,
|
|
|
|
|
+ then the `impl` must be declared in the same file so the existence of that
|
|
|
|
|
+ `impl` is visible to all files in that library.
|
|
|
|
|
+- Otherwise, the `impl` must be defined in the public API file of the library,
|
|
|
|
|
+ so it is visible in all places that might use it.
|
|
|
|
|
+
|
|
|
|
|
+No access control modifiers are allowed on `impl` declarations, an `impl` is
|
|
|
|
|
+always visible to the intersection of the visibility of all names used in the
|
|
|
|
|
+declaration of the `impl`.
|
|
|
|
|
+
|
|
|
## Generics
|
|
## Generics
|
|
|
|
|
|
|
|
Here is a function that can accept values of any type that has implemented the
|
|
Here is a function that can accept values of any type that has implemented the
|
|
@@ -1850,6 +1870,43 @@ class IntWrapper {
|
|
|
}
|
|
}
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
|
|
+### Use case: Private impl
|
|
|
|
|
+
|
|
|
|
|
+Adapter types can be used when a library publicly exposes a type, but only wants
|
|
|
|
|
+to say that type implements an interface as a private detail internal to the
|
|
|
|
|
+implementation of the type. In that case, instead of implementing the interface
|
|
|
|
|
+for the public type, the library can create a private adapter for that type and
|
|
|
|
|
+implement the interface on that instead. Any member of the class can cast its
|
|
|
|
|
+`me` parameter to the adapter type when it wants to make use of the private
|
|
|
|
|
+impl.
|
|
|
|
|
+
|
|
|
|
|
+```
|
|
|
|
|
+// Public, in API file
|
|
|
|
|
+class Complex64 {
|
|
|
|
|
+ // ...
|
|
|
|
|
+ fn CloserToOrigin[me: Self](them: Self) -> bool;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// Private
|
|
|
|
|
+
|
|
|
|
|
+adapter ByReal extends Complex64 {
|
|
|
|
|
+ // Complex numbers are not generally comparable,
|
|
|
|
|
+ // but this comparison function is useful for some
|
|
|
|
|
+ // method implementations.
|
|
|
|
|
+ impl as Comparable {
|
|
|
|
|
+ fn Less[me: Self](that: Self) -> bool {
|
|
|
|
|
+ return me.Real() < that.Real();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn Complex64.CloserToOrigin[me: Self](them: Self) -> bool {
|
|
|
|
|
+ var me_mag: ByReal = me * me.Conj() as ByReal;
|
|
|
|
|
+ var them_mag: ByReal = them * them.Conj() as ByReal;
|
|
|
|
|
+ return me_mag.Less(them_mag);
|
|
|
|
|
+}
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
### Adapter with stricter invariants
|
|
### Adapter with stricter invariants
|
|
|
|
|
|
|
|
**Future work:** Rust also uses the newtype idiom to create types with
|
|
**Future work:** Rust also uses the newtype idiom to create types with
|
|
@@ -3591,3 +3648,4 @@ parameter, as opposed to an associated type, as in `N:! u32 where ___ >= 2`.
|
|
|
- [#553: Generics details part 1](https://github.com/carbon-language/carbon-lang/pull/553)
|
|
- [#553: Generics details part 1](https://github.com/carbon-language/carbon-lang/pull/553)
|
|
|
- [#731: Generics details 2: adapters, associated types, parameterized interfaces](https://github.com/carbon-language/carbon-lang/pull/731)
|
|
- [#731: Generics details 2: adapters, associated types, parameterized interfaces](https://github.com/carbon-language/carbon-lang/pull/731)
|
|
|
- [#818: Constraints for generics (generics details 3)](https://github.com/carbon-language/carbon-lang/pull/818)
|
|
- [#818: Constraints for generics (generics details 3)](https://github.com/carbon-language/carbon-lang/pull/818)
|
|
|
|
|
+- [#931: Generic impls access (details 4)](https://github.com/carbon-language/carbon-lang/pull/931)
|