Wednesday, August 28, 2019

Kotlin in Action: Chapter 11 Exercises

Chapter 11 is about DSLs, and in addition to going over multiple example DSLs, it also gives us two more tools to use to assist in creating them: lambdas with receivers and the invoke convention. This is a topic that has a lot of depth to it, and we won't be able to cover all possibilities and options in a simple exercise, but with this exercise I hope to cover something with enough complexity to show the power and potential, while also keeping it simple enough to not be overwhelming.

Exercise: Family Tree

With this exercise we'll cover creating a simple family tree DSL. Note that for the sake of simplicity we'll stick with a very basic definition of a family where a father and mother have kids together, even though the definition of family can be much more complex in real life. So in this DSL, we can start by specifying a man or woman, and then in that person's definition a name can be specified, as well as father, mother, son, and daughter. For a man a wife can be specified, and for a woman a husband can be specified. Behind the scenes the DSL should hook up all the connections so that when, say, a father is specified, then the current person is added as a child of the person specified as the father, in addition to the father being added to the current person. So ultimately we should be able to specify code like the following with the DSL:
val john = man {
  name {
    first = "John"
    last = "Smith"
  }
  father {
    name {
      first = "Bob"
      middle = "Bobby"
      last = "Smith"
    }
  }
  mother {
    name {
      first = "Jane"
      last = "Doe"
    }
  }
  wife {
    name {
      first = "Martha"
      middle = "Molly"
      last = "May"
    }
  }
  son {
    name { first = "Joey"; last = "Smith" }
  }
  daughter {
    name { first = "Susie"; middle = "Que"; last = "Smith" }
  }
}

val sally = woman {
  name { first = "Sally" }
  husband {
    mother {
      father {
        name { first = "Richard" }
      }
    }
  }
}
As usual, answers to the exercise will be shared in a follow up post. And here's the answers.