Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Kotlin Properties and the Vaadin 8 Beanbinder [Code Snippet]

DZone's Guide to

Kotlin Properties and the Vaadin 8 Beanbinder [Code Snippet]

The Vaadin 8 beta is taking some lessons from Java 8's implementation of lambda expressions. But whereas Java doesn't recognize properties, Kotlin can help bind beans.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Vaadin 8 (currently in beta) comes with a whole new DataBinding API that makes heavy use of 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)


That's 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 and Vaadin!

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,kotlin ,vaadin ,beanbinder

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}