Skip to content

Instantly share code, notes, and snippets.

@MelbourneDeveloper
Created May 11, 2023 22:05
Show Gist options
  • Save MelbourneDeveloper/f734a8e08f0ff21657a54dc488d97053 to your computer and use it in GitHub Desktop.
Save MelbourneDeveloper/f734a8e08f0ff21657a54dc488d97053 to your computer and use it in GitHub Desktop.
Exhaustiveness
import 'dart:math';
sealed class Shape {}
class Square extends Shape {
Square(this.length, this.width);
final double length;
final double width;
}
class Circle extends Shape {
Circle(this.radius);
final double radius;
}
double calculateArea(Shape shape) => switch (shape) {
Square(length: var l, width: var w) => l * w,
Circle(radius: var r) => pi * r * r
};
void main() {
Shape shape = Square(3, 3);
print(calculateArea(shape));
shape = Circle(3);
print(calculateArea(shape));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment