Typing: Sealed Class
sealed class TShirtSize {}
class Small extends TShirtSize {}
class Medium extends TShirtSize {}
class ExtraLarge extends TShirtSize {
final int size;
ExtraLarge(this.size);
}
void printSizeInfo(TShirtSize theSize) {
// When not exhaustive, the compiler will refuse to compile
switch (theSize) {
case Small _:
print("Small");
case Medium _:
print("Medium");
case ExtraLarge val:
print("E" + "x" * val.size + "tra Large");
}
}
sealed class Role {
void printInfo() {
// When not exhaustive, the compiler will refuse to compile
switch (this) {
case Teacher teacher:
print(
"Name: ${teacher.name}, License: ${teacher.teachingLicense}, Major: ${teacher.teachingMajor}");
break;
case Student student:
print(
"Student: ${student.name}, ID: ${student.studentId}, School: ${student.school}");
break;
}
}
}
class Teacher extends Role {
final String name;
final String teachingLicense;
final String teachingMajor;
Teacher(this.name, this.teachingLicense, this.teachingMajor);
}
class Student extends Role {
final String name;
final String studentId;
final String school;
Student(this.name, this.studentId, this.school);
}
void main() {
printSizeInfo(Small()); // Output: Small
printSizeInfo(Medium()); // Output: Medium
printSizeInfo(ExtraLarge(1)); // Output: Extra Large
printSizeInfo(ExtraLarge(3)); // Output: Exxxtra Large
Teacher("John Doe", "123456", "Math")
.printInfo(); // Output: Name: John Doe, License: 123456, Major: Math
Student("Jane Doe", "123456", "University of Life")
.printInfo(); // Output: Student: Jane Doe, ID: 123456, School: University of Life
}The provided Dart code is straightforward and does the following:
- A
sealedclassTShirtSizeis declared, restricting inheritance to a specific set of classes defined in the same file. - Three classes
Small,Medium, andExtraLargeare defined that extend theTShirtSizesealed class. These represent specific t-shirt sizes. - The
ExtraLargeclass has an integersizefield, assigned via the constructor. - The
printSizeInfofunction accepts an instance ofTShirtSizeand uses an exhaustive switch case to print different outputs depending on the actual class of the instance. Dart’s exhaustiveness check on sealed classes ensures that all subclasses are covered. - A
sealedclassRoleis declared with aprintInfomethod, which prints different information based on whether the instance is aTeacheror aStudent. - The
TeacherandStudentclasses extend theRolesealed class. Each class has unique fields likename,teachingLicense,teachingMajorforTeacherandname,studentId,schoolforStudent. - In the
mainfunction, instances ofSmall,Medium, andExtraLargeare created and passed to theprintSizeInfofunction, printing “Small”, “Medium”, “Extra Large” and “Exxxtra Large” respectively. - The
mainfunction also creates instances ofTeacherandStudent, and calls theirprintInfomethod, printing “Name: John Doe, License: 123456, Major: Math” and “Student: Jane Doe, ID: 123456, School: University of Life” respectively.
The key takeaways from this code are the use of sealed classes for type safety and pattern matching, exhaustive switch cases for complete handling of all subclasses, and efficient printing of information based on the class of the instance.
Last updated on