concept compiler in category dsl

appears as: compiler, The compiler, compilers
DSLs in Action

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

Figure 2.4. An embedded typed DSL comes with lots of implicit guarantees of consistency. Use a type to model your DSL abstraction. The constraints that you define within your type are automatically checked by the compiler, even before the program runs.
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.