use_mixin_method_in_class_method.carbon 898 B

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