impl_self_interface_parameter.carbon 845 B

12345678910111213141516171819202122232425262728293031323334353637
  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 MyAddWith(T:! type) {
  11. fn Op[self: Self](b: T) -> Self;
  12. }
  13. class Point {
  14. var x: i32;
  15. var y: i32;
  16. }
  17. // Allowed: `Self` means `Point` after `as`
  18. impl Point as MyAddWith(Self) {
  19. fn Op[self: Point](b: Point) -> Point {
  20. return {.x = self.x + b.x, .y = self.y + b.y};
  21. }
  22. }
  23. fn DoAddGeneric[T:! type, U:! MyAddWith(T)](a: U, b: T) -> U {
  24. return a.Op(b);
  25. }
  26. fn Main() -> i32 {
  27. var a: Point = {.x = 2, .y = 1};
  28. var b: Point = {.x = 4, .y = 2};
  29. var p: Point = DoAddGeneric(a, b);
  30. return p.x - p.y - 3;
  31. }