convert.carbon 816 B

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