Functional Reactive UI Thing

October 22, 2011

Fruit is a Scala compiler plugin for building UI workflows declaratively using delimited continuations and functional reactive programming. Inspired by Deprecating the Observer Pattern.

To run the demo using Java 7, first build the Scala compiler plugin:

> sbt/sbt "fruit-plugin/publish-local"

Then run the demo:

> sbt/sbt "fruit-demos/run-main fruit.FruitDemo"

Consider the following UI:

Fruit screenshot 1
Fruit screenshot 1

This panel includes a combo box and a label. Changes to the combo box selection are reflected in the label:

Fruit screenshot 2
Fruit screenshot 2
Fruit screenshot 3
Fruit screenshot 3

Conventionally this would require adding to the combo box an ActionListener which would encapsulate the (relatively simple) logic of what to do with updates to the combo box selection; in this case, updating the text of the label. With Fruit, it is as simple as a one-line declaration:

label1.setText(signal(combo1))

This treats the combo box as a signal, and the text of the label is set to the time-varying value of the combo box signal. This can be used to build up more complex UI workflows:

label1.setText(signal(combo1))
label2.setText(signal(combo2))
label3.setText(signal(combo1) + " is " + signal(combo2))
label4.setText(signal(combo1) + " ain't " + signal(combo2))

This defines four labels which are updated based on the selection of the first combo box, the selection of the second combo box, or both. It keeps the UI workflow together, and is much easier to reason about than with observer implementations scattered about.

Fruit screenshot 4
Fruit screenshot 4
Fruit screenshot 5
Fruit screenshot 5
Fruit screenshot 6
Fruit screenshot 6
Fruit screenshot 7
Fruit screenshot 7
Fruit screenshot 8
Fruit screenshot 8