impl_self_interface_parameter.carbon 792 B

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