Files
sdk/pkg/front_end/testcases/patterns/pattern_matching.dart
Johnni Winther 763edcaf86 [_fe_analyzer_shared] Support switch expressions and errors in exhautiveness id testing
Change-Id: Ic1846f7a8b56fc72a816d9b6d4b930f80974759f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283321
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2023-02-16 11:53:16 +00:00

38 lines
946 B
Dart

// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:math' as math;
sealed class Shape {
double calculateArea();
}
class Square implements Shape {
final double length;
Square(this.length);
double calculateArea() => length * length;
}
class Circle implements Shape {
final double radius;
Circle(this.radius);
double calculateArea() => math.pi * radius * radius;
}
double calculateArea(Shape shape) => switch (shape) {
Square(length: var l) => l * l,
Circle(radius: var r) => math.pi * r * r
};
main() {
var s1 = Square(2);
expect(s1.calculateArea(), calculateArea(s1));
var s2 = Circle(3);
expect(s2.calculateArea(), calculateArea(s2));
}
expect(expected, actual) {
if (expected != actual) throw "Expected $expected, actual $actual";
}