Class Modifiers
Can construct?
Can extend (outside the library)?
Can implement (outside the library)?
Partially implemented base ✅ (subclasses must be base/final/sealed)
Ensures implementation inheritance (invariants preserved) Closed type hierarchy ❌ (implicitly abstract) ❌ (only in the same library)
Exhaustive subtype checking in switch Class and mixin abstract interface
Restrictions apply outside the declaring library. Within the same library you can still extend/implement.
sealed class Result<T> {}
final class Success<T> extends Result<T> {
final T value;
Success(this.value);
}
final class Failure<T> extends Result<T> {
final Object error;
Failure(this.error);
}
String render(Result<int> result) => switch (result) { // exhaustive: no default needed
Success(:final value) => 'Got $value',
Failure(:final error) => 'Error: $error',
};
sealed class Shape {}
class Circle extends Shape { final double r; Circle(this.r); }
class Rect extends Shape { final double w, h; Rect(this.w, this.h); }
double area(Shape s) => switch (s) {
Circle(r: var r) => 3.14159 * r * r,
Rect(:var w, :var h) => w * h,
}; // adding a new Shape subtype makes this a compile error until it's handled