Created
May 11, 2023 22:05
-
-
Save MelbourneDeveloper/f734a8e08f0ff21657a54dc488d97053 to your computer and use it in GitHub Desktop.
Exhaustiveness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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