use_mixin_method_in_class_method.carbon 949 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. fn Square[self: Self](x:i32) -> i32{
  12. return x * x;
  13. }
  14. }
  15. class Point {
  16. var x: i32;
  17. var y: i32;
  18. fn DistanceSquare[self: Self](other:Self) -> i32 {
  19. return self.Square(self.x - other.x) + self.Square(self.y - other.y);
  20. }
  21. __mix Operations;
  22. }
  23. class Complex {
  24. var r: i32;
  25. var i: i32;
  26. __mix Operations;
  27. fn AbsSquare[self: Self]() -> i32 {
  28. return self.Square(self.r) + self.Square(self.i);
  29. }
  30. }
  31. fn Main() -> i32 {
  32. var p1: Point = {.x = 1, .y = 2 };
  33. var p2: Point = {.x = 4, .y = 3 };
  34. var c: Complex = {.r = 5, .i = 6 };
  35. return c.AbsSquare() - p1.DistanceSquare(p2) - 51;
  36. }