conversion.carbon 773 B

12345678910111213141516171819202122232425262728293031323334
  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: 1
  7. // CHECK:STDOUT: 2
  8. // CHECK:STDOUT: 3
  9. // CHECK:STDOUT: 4
  10. // CHECK:STDOUT: result: 0
  11. package ExplorerTest api;
  12. class A {
  13. var n: i32;
  14. impl as ImplicitAs(i32) {
  15. fn Convert[self: Self]() -> i32 { return self.n; }
  16. }
  17. }
  18. impl i32 as ImplicitAs(A) {
  19. fn Convert[self: Self]() -> A { return {.n = self}; }
  20. }
  21. fn Main() -> i32 {
  22. var x: {.a: A, .b: i32} = {.a = 1, .b = 2 as A};
  23. Print("{0}", x.a.n);
  24. Print("{0}", x.b);
  25. var y: {.a: i32, .b: A} = {.a = 3, .b = 4 as A};
  26. x = y;
  27. Print("{0}", x.a.n);
  28. Print("{0}", x.b);
  29. return 0;
  30. }