omit_self.carbon 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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: result: 0
  9. package ExplorerTest api;
  10. interface Vector {
  11. fn Zero() -> Self;
  12. fn Add[me: Self](b: Self) -> Self;
  13. fn Scale[me: Self](v: i32) -> Self;
  14. }
  15. class Point {
  16. var x: i32;
  17. var y: i32;
  18. // Allowed: means the same as `impl Self as Vector`
  19. // or `impl Point as Vector`.
  20. impl as Vector {
  21. fn Zero() -> Point {
  22. return {.x = 0, .y = 0};
  23. }
  24. fn Add[me: Point](b: Point) -> Point {
  25. return {.x = me.x + b.x, .y = me.y + b.y};
  26. }
  27. fn Scale[me: Point](v: i32) -> Point {
  28. return {.x = me.x * v, .y = me.y * v};
  29. }
  30. }
  31. }
  32. fn AddAndScaleGeneric[T:! Vector](a: T, s: i32) -> T {
  33. return a.Add(T.Zero()).Scale(s);
  34. }
  35. fn Main() -> i32 {
  36. var a: Point = {.x = 2, .y = 1};
  37. var p: Point = AddAndScaleGeneric(a, 5);
  38. return p.x - 10;
  39. }