-
-
Save humblerookie/b0f36d6dd086ec9692b4a2dcd249be31 to your computer and use it in GitHub Desktop.
Property delegates with another field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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