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