impl_in_generic_class.carbon 904 B

12345678910111213141516171819202122232425262728293031323334
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // NOAUTOUPDATE
  6. package ExplorerTest api;
  7. interface I(T:! type) {
  8. fn F[self: Self](t: T, o: Self) -> Self;
  9. }
  10. class X(U:! type) {
  11. extend impl as I(U) {
  12. fn F[self: Self](u: U, o: Self) -> Self { return {.m = u, .n = self.n + o.n}; }
  13. }
  14. var m: U;
  15. var n: i32;
  16. }
  17. fn Run[V:! type](x: V) -> (V, V, i32, i32) {
  18. var v: X(V) = {.m = x, .n = 1};
  19. var w: X(V) = {.m = x, .n = 2};
  20. // OK, know that `X(V)` implements `I(V)` from the `impl` in the class.
  21. var call: X(V) = v.(X(V).(I(V).F))(x, w);
  22. return (call.(X(V).m), call.m, call.(X(V).n), call.n);
  23. }
  24. fn Main() -> i32 {
  25. var (a: i32, b: i32, c: i32, d: i32) = Run(1);
  26. return a * 1000 + b * 100 + c * 10 + d;
  27. }
  28. // CHECK:STDOUT: result: 1133