Skip to content

Instantly share code, notes, and snippets.

@humblerookie

humblerookie/Vehicle.kt Secret

Last active Jul 19, 2020
Embed
What would you like to do?
Property delegates with another field
class Vehicle(val type: VehicleType = CYCLE) {
@Deprecated("Use 'type', instead", ReplaceWith("type"))
val isCar: Boolean by this::type
@Deprecated("Use constructor with 'type' info instead")
constructor (isCar: Boolean) : this(if (isCar) CAR else CYCLE)
enum class VehicleType {
CYCLE, CAR, TRUCK, SHIP
}
private operator fun KProperty<*>.getValue(
vehicle: Vehicle,
property: KProperty<*>
): Boolean {
return vehicle.type == CAR
}
}
fun main() {
val vehicle = Vehicle(TRUCK)
//Below line would throw a deprecated warning
println(vehicle.isCar)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment