impl_as.carbon 750 B

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