self_substitution.carbon 841 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. __mixin Operations {
  8. // Here Self which is both the input and output type is a type variable
  9. fn F[self: Self](x: Self) -> Self {
  10. return x;
  11. }
  12. }
  13. class Point {
  14. fn Origin() -> Point {
  15. return {.x = 0, .y = 0};
  16. }
  17. var x: i32;
  18. var y: i32;
  19. __mix Operations;
  20. }
  21. fn Main() -> i32 {
  22. var p1: Point = Point.Origin();
  23. var p2: Point = {.x = 42, .y = 1};
  24. // After accessing the mixin member F through a Point object, the
  25. // input and output type variables of F get substituted with
  26. // the Point class type
  27. var p3: Point = p1.F(p2);
  28. return p3.x - 42;
  29. }
  30. // CHECK:STDOUT: result: 0