Skip to content

Instantly share code, notes, and snippets.

@AlexVegner
Created January 24, 2020 09:05
Show Gist options
  • Select an option

  • Save AlexVegner/7ae4d9e2b4921a97bdf52696353d528b to your computer and use it in GitHub Desktop.

Select an option

Save AlexVegner/7ae4d9e2b4921a97bdf52696353d528b to your computer and use it in GitHub Desktop.
dart_mixin.dart
import 'dart:math' as math;
class Point {
final int x;
final int y;
const Point(this.x, this.y);
}
// class / abstract class can be used as mixin
class LastPoint {
bool last = false;
}
// Mixin depends on class
mixin Distance on Point {
double distance(Point p) {
final a = (x - p.x).abs();
final b = (y - p.y).abs();
return math.sqrt(a*a + b*b);
}
}
class PointWithDistance extends Point with Distance, LastPoint {
PointWithDistance(int x, int y) : super(x, y);
}
void main() {
final p1 = PointWithDistance(0, 3);
final p2 = PointWithDistance(4, 0)
..last = true;
final distance = p1.distance(p2);
print(distance);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment