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