member_access.carbon 634 B

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