Skip to content

Instantly share code, notes, and snippets.

@humblerookie
Last active July 19, 2020 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humblerookie/b0f36d6dd086ec9692b4a2dcd249be31 to your computer and use it in GitHub Desktop.
Save humblerookie/b0f36d6dd086ec9692b4a2dcd249be31 to your computer and use it in GitHub Desktop.
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