member_access.carbon 688 B

1234567891011121314151617181920212223242526272829303132
  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: 1
  9. // CHECK:STDOUT: 3
  10. // CHECK:STDOUT: result: 0
  11. package ExplorerTest api;
  12. interface GetX {
  13. fn DoIt[self: Self]() -> i32;
  14. }
  15. impl forall [template T:! type] T as GetX {
  16. fn DoIt[self: Self]() -> i32 { return self.x; }
  17. }
  18. class C {
  19. var x: i32;
  20. }
  21. fn Main() -> i32 {
  22. var a: auto = {.x = 1, .y = 2};
  23. var b: C = {.x = 3};
  24. Print("{0}", a.(GetX.DoIt)());
  25. Print("{0}", b.(GetX.DoIt)());
  26. return 0;
  27. }