impl_as.carbon 794 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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: Carrot.G
  9. // CHECK:STDOUT: result: 5
  10. package ExplorerTest api;
  11. interface Apple(T:! Type) {
  12. fn F[me: Self]() -> T;
  13. }
  14. interface Banana {
  15. impl as Apple(i32);
  16. fn G[me: Self]();
  17. }
  18. class Carrot {
  19. var n: i32;
  20. }
  21. external impl Carrot as Apple(i32) {
  22. fn F[me: Self]() -> i32 { return me.n; }
  23. }
  24. external impl Carrot as Banana {
  25. fn G[me: Self]() { Print("Carrot.G"); }
  26. }
  27. fn H[T:! Banana](x: T) -> i32 {
  28. x.G();
  29. return x.(Apple(i32).F)();
  30. }
  31. fn Main() -> i32 {
  32. var c: Carrot = {.n = 5};
  33. return H(c);
  34. }