Use Inner Classes to Share State Between Contexts

On one hand, we want to maintain multiple contexts so that we can control the sequence of function calls that should be permitted by the DSL. On the other hand, the function calls, executing in different contexts, have to come together. By working together, they can represent a cohesive set of data or state that may be created by the sequence of instructions. In this section, we’ll see how to realize those two goals at the same time.

Let’s design a DSL to schedule meetings. A user of our DSL will be able to schedule them using a lightweight syntax like this one:

 schedule meeting {
  assign name ​"Meeting to discuss why meetings aren't effective"
  starts at 14..30
  ends ...

Get Programming DSLs in Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.