conversion.carbon 776 B

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