Chapter 46. Literal Extension

Add methods to program literals.

 42.grams.flour

46.1 How It Works

Literals, such as numbers and strings, often make a good starting point for DSL expressions. Traditionally, however, they are built-in types with fixed interfaces, so you can’t extend them. More languages now allow you to add methods to third-party classes using techniques like C#’s extension methods and Ruby’s open classes. This capability is particularly handy for DSLs, as it allows you to start a method chain with a literal.

As with most method chains, one important decision is whether to use an Expression Builder. If you don’t use an Expression Builder, you have to ensure that all the intermediate types have the appropriate fluent methods defined ...

Get Domain Specific Languages 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.