Chapter 5. Internal DSL design in Ruby, Groovy, and Clojure

 

This chapter covers

  • Making DSLs concise using duck typing and metaprogramming
  • Implementing a trade-processing DSL in Ruby
  • Improving our order-processing DSL using Groovy
  • Thinking differently about the trade-processing DSL in Clojure
  • Common pitfalls with each language

The best way to learn new paradigms and design techniques is to look at real implementations using the best languages that support them. In chapter 4, you saw quite a few idioms and patterns that can help you develop expressive internal DSLs. For this chapter, I’ve selected three of today’s most popular languages on the JVM. You’re going to go through the exercise of building real-world DSLs using them.

Before going into the details of what we’re going to do, figure 5.1 is a roadmap that I plan to follow in this chapter.

Figure 5.1. Roadmap of the chapter

The languages I’ve selected for this chapter are dynamically typed. I start the chapter by discussing some of the attributes of dynamically typed languages that make good DSLs and why I selected Ruby, Groovy, and Clojure for our discussion. Then we’ll jump right into the implementation part and take you successively through the process of implementing complete DSLs using each of the three languages. We discuss the main features that these languages offer that you’ll frequently use in designing DSLs and some of the rationale for when to select which pattern for your implementation.

5.1. Making DSLs concise with dynamic typing

5.2. A trade-processing DSL in Ruby

5.3. The order-processing DSL: the final frontier in Groovy

5.4. Thinking differently in Clojure

5.5. Recommendations to follow

5.6. Summary

5.7. References