addr_self.carbon 1.0 KB

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