generic_call_generic.carbon 974 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 Add[self: Self](b: Self) -> Self;
  9. fn Scale[self: Self](v: i32) -> Self;
  10. }
  11. class Point {
  12. var x: i32;
  13. var y: i32;
  14. extend impl as Vector {
  15. fn Add[self: Point](b: Point) -> Point {
  16. return {.x = self.x + b.x, .y = self.y + b.y};
  17. }
  18. fn Scale[self: Point](v: i32) -> Point {
  19. return {.x = self.x * v, .y = self.y * v};
  20. }
  21. }
  22. }
  23. fn ScaleGeneric[U:! Vector](c: U, s: i32) -> U {
  24. return c.Scale(s);
  25. }
  26. fn AddAndScaleGeneric[T:! Vector](a: T, b: T, s: i32) -> T {
  27. return ScaleGeneric(a.Add(b), s);
  28. }
  29. fn Main() -> i32 {
  30. var a: Point = {.x = 1, .y = 1};
  31. var b: Point = {.x = 2, .y = 3};
  32. var p: Point = AddAndScaleGeneric(a, b, 5);
  33. return p.x - 15;
  34. }
  35. // CHECK:STDOUT: result: 0