Is there a better way to do read only properties? ...
# tornadofx
m
Is there a better way to do read only properties?
Copy code
private val isRunningWrapperProperty = ReadOnlyBooleanWrapper(false)
    var isRunning by isRunningWrapperProperty
        private set
    fun isRunningProperty(): ReadOnlyBooleanProperty = isRunningWrapperProperty.readOnlyProperty
m
Except for the minor detail of using:
Copy code
val isRunningProperty: ReadOnlyBooleanProperty get() = isRunningWrapperProperty.readOnlyProperty
instead of a function (if you don't care how you access it from Java), I don't think there is any better way. Another variant is the following, separating public and private interface:
Copy code
private val isRunningImplProperty = ReadOnlyBooleanWrapper(false)
    private var isRunningImpl by isRunningImplProperty 

    val isRunning by isRunningWrapperProperty
    val isRunningProperty: ReadOnlyBooleanProperty get() = isRunningWrapperProperty.readOnlyProperty
Again, nothing very meaningful. Just matter of taste. I don't see how you could gain anything even using a custom delegation.
j
Sometimes there is no reference necessary to the writable property because an "apply" is enough to bind it:
Copy code
val runningProperty: ReadOnlyBooleanProperty = SimpleBooleanProperty().also{ [bind me]}
val running by val runningProperty