addr_self.carbon 1.0 KB

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