October 2021
Intermediate to advanced
500 pages
16h 23m
English
Algebraic data types (or ADTs, for short) allow you to represent a closed set of possible subtypes that can be associated with a given type. Enum classes are a simple form of ADT.
Imagine a Student class that has three possible associated states, depending on the student’s enrollment status: NotEnrolled, Active, or Graduated.
Using the enum class that you learned about in this chapter, you could model the three states for the Student class as follows:
enum class StudentStatus {
NotEnrolled,
Active,
Graduated
}
class Student(var status: StudentStatus)
fun main() {
val student = Student(StudentStatus.NotEnrolled)
}
And you could write a function that generates a student message using ...
Read now
Unlock full access