generic_with_two_params.carbon 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // RUN: %{executable_semantics} %s 2>&1 | \
  6. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s
  7. // RUN: %{executable_semantics} --parser_debug --trace_file=- %s 2>&1 | \
  8. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s
  9. // AUTOUPDATE: %{executable_semantics} %s
  10. // CHECK: result: 0
  11. package ExecutableSemanticsTest api;
  12. interface Vector {
  13. fn Add[me: Self](b: Self) -> Self;
  14. fn Scale[me: Self](v: i32) -> Self;
  15. }
  16. class Point1 {
  17. var x: i32;
  18. var y: i32;
  19. impl Point1 as Vector {
  20. fn Add[me: Point1](b: Point1) -> Point1 {
  21. return {.x = me.x + b.x, .y = me.y + b.y};
  22. }
  23. fn Scale[me: Point1](v: i32) -> Point1 {
  24. return {.x = me.x * v, .y = me.y * v};
  25. }
  26. }
  27. }
  28. class Point2 {
  29. var x: i32;
  30. var y: i32;
  31. impl Point2 as Vector {
  32. fn Add[me: Point2](b: Point2) -> Point2 {
  33. return {.x = me.x + b.x + 1, .y = me.y + b.y + 1};
  34. }
  35. fn Scale[me: Point2](v: i32) -> Point2 {
  36. return {.x = me.x * v * 2, .y = me.y * v * 2};
  37. }
  38. }
  39. }
  40. fn ScaleGeneric[U:! Vector](c: U, s: i32) -> U {
  41. return c.Scale(s);
  42. }
  43. fn AddAndScaleGeneric[T:! Vector, V:! Vector](a: T, b: V, s: i32) -> (T, V) {
  44. return (ScaleGeneric(a.Add(a), s),
  45. ScaleGeneric(b.Add(b), s));
  46. }
  47. fn Main() -> i32 {
  48. var a: Point1 = {.x = 1, .y = 1};
  49. var b: Point2 = {.x = 2, .y = 3};
  50. var (p: Point1, q: Point2) = AddAndScaleGeneric(a, b, 5);
  51. return q.x - p.x - 40;
  52. }