With the two previous sections (infix
functions and operator overloading), we have a good foundation for building fantastic DSLs. A DSL is a language that is specialized to a particular domain, in contrast to general-purpose language (GPL). Classic examples of DSLs (even when people don't realize it) are HTML (markup) and SQL (relational database queries).
Kotlin provides many features to create internal DSLs (a DSL that runs internally inside a host GPL), but there is one feature that we still need to cover, type-safe builders. Type-safe builders let us define data in a (semi) declarative way and are very useful to define GUIs, HTML markup, XML, and others.
An example of a beautiful Kotlin DSL is TornadoFX. TornadoFX (https://tornadofx.io/) is DSL for creating JavaFX applications.
We write an FxApp
class that extends tornadofx.App
and receives a tornadofx.View
class (a class reference, not an instance):
import javafx.application.Application import tornadofx.* fun main...