Vetoable properties in Kotlin

Vetoable are delegate properties that allow you to update a property value if a certain condition is matched.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
private var vetoableValue: String by Delegates.vetoable("Real Kotlin") {
    // Will only update if the proposed value starts with an R
    _, _, new -> new.startsWith("R")
}

fun failVetoableCondition(): String {
    vetoableValue = "Won't update"
    return vetoableValue
}

fun passVetoableCondition(): String {
    vetoableValue = "Royal Wedding"
    return vetoableValue
}

fun main(args: Array<String>) {
    println(vetoableValue)
    // Real kotlin

    println(failVetoableCondition())
    // Real Kotlin

    println(passVetoableCondition())
    // Royal Wedding
}

The value will only change once we hit a condition set by the property.

Updated: