Model success and rejection with sealed subclasses.

result-model Sealed classes are useful when a function has a small set of possible result shapes.

Result Modeling

points
ResultModeling.kt
Replay: real traced execution (multi-file project)
sealed class CheckResult
data class Accepted(val points: Int) : CheckResult()
data class Rejected(val reason: String) : CheckResult()

fun check(points: Int): CheckResult {
    if (points >= 5) {
        return Accepted(points)
    }
    return Rejected("too low")
}

fun describe(result: CheckResult): String {
    if (result is Accepted) {
        return "accepted:${result.points}"
    }
    if (result is Rejected) {
        return "rejected:${result.reason}"
    }
    return "unknown"
}

fun main() {
    val points = 7
    val result = check(points)
    val label = describe(result)

    println("points=$points")
    println("label=$label")
}
sealed class CheckResult
data class Accepted(val points: Int) : CheckResult()
data class Rejected(val reason: String) : CheckResult()

fun check(points: Int): CheckResult {
    if (points >= 5) {
        return Accepted(points)
    }
    return Rejected("too low")
}

fun describe(result: CheckResult): String {
    if (result is Accepted) {
        return "accepted:${result.points}"
    }
    if (result is Rejected) {
        return "rejected:${result.reason}"
    }
    return "unknown"
}

fun main() {
    val points = 2
    val result = check(points)
    val label = describe(result)

    println("points=$points")
    println("label=$label")
}
sealed class CheckResult
data class Accepted(val points: Int) : CheckResult()
data class Rejected(val reason: String) : CheckResult()

fun check(points: Int): CheckResult {
    if (points >= 5) {
        return Accepted(points)
    }
    return Rejected("too low")
}

fun describe(result: CheckResult): String {
    if (result is Accepted) {
        return "accepted:${result.points}"
    }
    if (result is Rejected) {
        return "rejected:${result.reason}"
    }
    return "unknown"
}

fun main() {
    val points = 10
    val result = check(points)
    val label = describe(result)

    println("points=$points")
    println("label=$label")
}
  1. points ← 7

    22fun main() {23    val points→ 7 = 7 //@points=2, 1024    val result = check(points7)25    val label = describe(result)
  2. fun check(points: Int): CheckResult

    5fun check(points7: Int): CheckResult {6    if (points >= 5) {
  3. if (points >= 5)

    5fun check(points: Int): CheckResult {6    if (points7 >= 5) {7        return Accepted(points7)8    }
  4. val result = check(points)

    23val points = 7 //@points=2, 1024val result = check(points7)25val label = describe(result)
  5. fun describe(result: CheckResult): String

    12fun describe(result: CheckResult): String {13    if (result is Accepted) {
  6. if (result is Accepted)

    12fun describe(result: CheckResult): String {13    if (result is Accepted) {14        return "accepted:${result.points7}"15    }
  7. label ← accepted:7

    24    val result = check(points)25    val label→ accepted:7 = describe(result)2627    println("points=$points7")28    println("label=$labelaccepted:7")29}
    outputpoints=7
    label=accepted:7
  1. points ← 2

    22fun main() {23    val points→ 2 = 224    val result = check(points2)25    val label = describe(result)
  2. fun check(points: Int): CheckResult

    5fun check(points2: Int): CheckResult {6    if (points >= 5) {7        return Accepted(points)8    }9    return Rejected("too low")10}
  3. val result = check(points)

    23val points = 224val result = check(points2)25val label = describe(result)
  4. fun describe(result: CheckResult): String

    12fun describe(result: CheckResult): String {13    if (result is Accepted) {
  5. if (result is Rejected)

    15}16if (result is Rejected) {17    return "rejected:${result.reasontoo low}"18}
  6. label ← rejected:too low

    24    val result = check(points)25    val label→ rejected:too low = describe(result)2627    println("points=$points2")28    println("label=$labelrejected:too low")29}
    outputpoints=2
    label=rejected:too low
  1. points ← 10

    22fun main() {23    val points→ 10 = 1024    val result = check(points10)25    val label = describe(result)
  2. fun check(points: Int): CheckResult

    5fun check(points10: Int): CheckResult {6    if (points >= 5) {
  3. if (points >= 5)

    5fun check(points: Int): CheckResult {6    if (points10 >= 5) {7        return Accepted(points10)8    }
  4. val result = check(points)

    23val points = 1024val result = check(points10)25val label = describe(result)
  5. fun describe(result: CheckResult): String

    12fun describe(result: CheckResult): String {13    if (result is Accepted) {
  6. if (result is Accepted)

    12fun describe(result: CheckResult): String {13    if (result is Accepted) {14        return "accepted:${result.points10}"15    }
  7. label ← accepted:10

    24    val result = check(points)25    val label→ accepted:10 = describe(result)2627    println("points=$points10")28    println("label=$labelaccepted:10")29}
    outputpoints=10
    label=accepted:10