concept Scala in category dsl

appears as: Scala, Scala
DSLs in Action

This is an excerpt from Manning's book DSLs in Action.

Now that you’re comfortable with the three common models of execution for a DSL script, revisit the DSL in listing 1.1 that Bob was playing with. Irrespective of the language of implementation, you’ll discover that it also needs a semantic model as its underlying implementation. That model might be a host language like Ruby or Scala, or it might be a custom language that the developers at Trampoline Securities designed to implement the trading DSL.

Like all implementation discussions, this is going to be a programming-intensive chapter in which we’ll start with the suitability of Scala as a host for implementing internal DSLs, and then dive right into real-world DSL design. Figure 6.1 is a roadmap of our journey through this chapter.

Figure 6.1. Our roadmap through this chapter

Sections 6.1 and 6.2 will establish Scala as a host for internal DSLs. After that, we get into the details of implementing real-world use cases from our domain of securities trading back-office systems. You’ll see lots of idioms, best practices, and patterns in action in sections 6.3 through 6.6. In section 6.7, we’ll discuss how you can compose multiple DSLs to evolve larger ones. We’ll conclude the chapter with a discussion about how monads can make your DSLs concise, functional, and more expressive.

At the end of the chapter, you’ll have comprehensive knowledge of how to design DSLs using Scala as the host language. You’ll learn the idioms and best practices of how to model domain components and how to create easy-to-use and expressive language abstractions with them. Sounds exciting, doesn’t it? Let’s get started.

I used Scala 2.8 to produce the code listings and snippets. For those of you not familiar with the Scala syntax, there’s a Scala cheat sheet for you in appendix D at the end of the book.

Scala offers a host of functional, as well as OO, abstraction mechanisms that help you design concise and expressive DSLs. A DSL can’t stand on its own; in case you missed it before, a DSL is layered as a facade on top of an implementation model. In this section, we’ll take a look at how Scala shapes up as a host language, both for designing the underlying model as well as the DSL layer on top of it. Table 6.1 shows some of the features of idiomatic Scala that you’ll use regularly to design your DSLs.

Table 6.1. Idiomatic Scala in DSL design

Feature

How Scala does it

Flexible syntax Scala has a concise surface syntax, with many features that help morph your DSL to use the more natural dialect of the domain. Examples:
  • Optional dots in method invocation
  • Semicolon inference
  • Infix operators
  • Optional parentheses
An extensible object system Scala is object-oriented. It shares Java’s object model and extends it on many fronts through its advanced type system. Scala’s object semantics:
  • Traits for mixin-based implementation inheritance (see [12] in section 6.10)
  • Orthogonal extension capabilities of classes through abstract type members and generic type parameters (see [13] in section 6.10)
  • Constrained orthogonality of abstractions through self-type annotations (see [14] in section 6.10)
  • Case classes for implementing value objects [1]

    1 The main differences between ordinary classes and case classes are simpler constructor invocation, availability of default equality semantics, and pattern matching (see [2] in section 6.10).

Functional programming capabilities Scala is a multi-paradigm programming language. It combines the power of OO and functional programming features. Why object-functional?
  • Functions in Scala are first-class values; higher-order functions are supported at the type-system level. You can define custom DSL control structures as closures and pass them around just like any other data type.
  • With a pure OO language, you need to design everything as classes, whether it is a verb or a noun in your domain. Scala’s OO functional mix allows you to model closer to the semantics of your problem domain.
Statically checked duck typing Scala supports duck typing through structural types (see [2] in section 6.10). Difference with Ruby duck-typing: Duck typing in Scala is statically checked.
Lexically scoped open classes Scala offers the power of open classes through its implicit construct, as we discussed in the Scala implicits sidebar in section 3.2. Difference with Ruby monkey patching: Scala implicits are lexically scoped; the added behavior via implicit conversions needs to be explicitly imported into specific lexical scopes (see [2] in section 6.10).
Implicit parameters Allow the compiler to infer some of the arguments implicitly without your having to specify them as part of your API invocation. Doing so leads to concise syntax and improved readability for your DSL script.
Modular composition A distinct notion of an object, which you can use to define a concrete module of your DSL. You can define DSL constructs in terms of abstract members and defer committing to a concrete implementation until late in your abstraction design lifecycle.

The confluence of the features listed in table 6.1 makes Scala one of the most powerful languages on the JVM for designing internal DSLs. But it’s still a new language that makes you think differently. Introducing a new language to a team is always a big change, both technologically and culturally. Your company might already have made a big commitment to the JVM as a platform, a number of your clients’ applications run on Java, and you have a whole lot of programmers trained in myriad Java frameworks. Are you going to forego all the benefits that you’ve accrued in order to embrace a new programming language? Fortunately, with Scala, you can afford to take an incremental step toward this transition. The next section discusses how.