convert.carbon 885 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: 2
  9. // CHECK:STDOUT: 3
  10. // CHECK:STDOUT: 4
  11. // CHECK:STDOUT: 5
  12. // CHECK:STDOUT: result: 0
  13. package ExplorerTest api;
  14. class A {
  15. var a: i32;
  16. }
  17. class B {
  18. var b: i32;
  19. }
  20. class C {
  21. var c: i32;
  22. }
  23. external impl A as ImplicitAs(B) {
  24. fn Convert[self: Self]() -> B { return {.b = self.a + 1}; }
  25. }
  26. external impl B as As(C) {
  27. fn Convert[self: Self]() -> C { return {.c = self.b + 2}; }
  28. }
  29. fn Main() -> i32 {
  30. var a: (A, A) = ({.a = 1}, {.a = 2});
  31. var b: (B, B) = a;
  32. Print("{0}", b[0].b);
  33. Print("{0}", b[1].b);
  34. var c: (C, C) = b as (C, C);
  35. Print("{0}", c[0].c);
  36. Print("{0}", c[1].c);
  37. return 0;
  38. }