addr_self.carbon 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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: 75
  9. // CHECK:STDOUT: 100
  10. // CHECK:STDOUT: result: 0
  11. package ExplorerTest api;
  12. interface Vector {
  13. fn Zero() -> Self;
  14. fn Add[addr self: Self*](b: Self);
  15. fn Scale[addr self: Self*](v: i32);
  16. }
  17. class Point {
  18. var x: i32;
  19. var y: i32;
  20. impl as Vector {
  21. fn Zero() -> Self {
  22. return {.x = 1, .y = 1};
  23. }
  24. fn Add[addr self: Self*](b: Self) {
  25. (*self).x = (*self).x + b.x;
  26. (*self).y = (*self).y + b.y;
  27. }
  28. fn Scale[addr self: Self*](v: i32) {
  29. (*self).x = (*self).x * v;
  30. (*self).y = (*self).y * v;
  31. }
  32. }
  33. }
  34. fn AddAndScaleGeneric[T:! Vector](p: T*, s: i32) {
  35. (*p).Add(T.Zero());
  36. (*p).(Vector.Scale)(s);
  37. (*p).(T.(Vector.Scale))(s);
  38. }
  39. fn Main() -> i32 {
  40. var a: Point = {.x = 2, .y = 3};
  41. AddAndScaleGeneric(&a, 5);
  42. Print("{0}", a.x);
  43. Print("{0}", a.y);
  44. return 0;
  45. }