I'm writing some JavaFX code, and this framework h...
# getting-started
l
I'm writing some JavaFX code, and this framework has the annoying habit of using javabeans properties a lot. The Java method of declaring a member involves subclassing
ObjectPropertyBase
, create a function called
fooProperty
that returns an instance of this class, and then create
getFoo()
and
setFoo()
methods that delegate to this property. I've been trying to make a nicer way to achieve this by using Kotlin delegates, but my result is not much shorter than the Java style. Surely there must be some neat way to achieve this? Is this something someone has already done?
d
I don't really know about JavaFX, but what about using
SimpleObjectProperty
instead?
e
if you want property delegation like
Copy code
class MyBean {
    var foo: T by SimpleObjectProperty(this, "foo")
}
you just need some functions in scope like
Copy code
operator fun <T> ReadableProperty<T>.getValue(thisRef: Any?, property: KProperty<*>): T =
    getValue()
operator fun <T> WritableProperty<T>.setValue(thisRef: Any?, property: KProperty<*>, value: T) {
    setValue(value)
}
l
@ephemient wait, so I can add those as extension functions?
e
yes they just need to be in scope at the use site
l
Interesting. Let me try this.