Sunday, January 15, 2017

Kotlin Properties and the Vaadin 8 BeanBinder

Vaadin 8 (currently in beta) comes with a whole new DataBinding API that makes heavy use of the Java 8 Lambda Features.

Unfortunately Java has no concept of a Property, so one of the new ways to bind a Bean Property is writing something like (Taken from the documentation):

binder.bind(nameField, Person::getName, Person::setName);

Since Kotlin has a concept for a property you may want to write something like:

binder.forField(nameField)
        .bind(Person::name)


Thats possible with an extension method for the Vaadin Binder that could probably look like this:

fun <BEAN, T> Binder.BindingBuilder<BEAN, T>.bind(prop: KMutableProperty<T>) {
    this.bind(
            ValueProvider { bean: BEAN -> prop.getter.call(bean) },
            Setter { bean: BEAN, v: T -> prop.setter.call(bean, v) })
}

Have fun with Kotlin & Vaadin!




No comments:

Using Mapstruct with Protobuf3