impl_self_interface_parameter.carbon 791 B

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