July 2018
Intermediate to advanced
400 pages
12h 14m
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: NOT_ENROLLED, 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 {
NOT_ENROLLED,
ACTIVE,
GRADUATED
}
class Student(var status: StudentStatus)
fun main(args: Array<String>) {
val student = Student(StudentStatus.NOT_ENROLLED)
}
And you could write a function that generates a ...
Read now
Unlock full access