impl_in_generic_class.carbon 948 B

1234567891011121314151617181920212223242526272829303132333435
  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. // AUTOUPDATE
  6. // RUN: %{explorer-run}
  7. // RUN: %{explorer-run-trace}
  8. // CHECK:STDOUT: result: 1133
  9. package ExplorerTest api;
  10. interface I(T:! type) {
  11. fn F[self: Self](t: T, o: Self) -> Self;
  12. }
  13. class X(U:! type) {
  14. impl as I(U) {
  15. fn F[self: Self](u: U, o: Self) -> Self { return {.m = u, .n = self.n + o.n}; }
  16. }
  17. var m: U;
  18. var n: i32;
  19. }
  20. fn Run[V:! type](x: V) -> (V, V, i32, i32) {
  21. var v: X(V) = {.m = x, .n = 1};
  22. var w: X(V) = {.m = x, .n = 2};
  23. // OK, know that `X(V)` implements `I(V)` from the `impl` in the class.
  24. var call: X(V) = v.(X(V).(I(V).F))(x, w);
  25. return (call.(X(V).m), call.m, call.(X(V).n), call.n);
  26. }
  27. fn Main() -> i32 {
  28. var (a: i32, b: i32, c: i32, d: i32) = Run(1);
  29. return a * 1000 + b * 100 + c * 10 + d;
  30. }