liveBook · Manning

Expression

As explained in Building User-Friendly DSLs

A domain-specific-language syntactic construct whose instances evaluate to values of specified types.

An expression is DSL content that evaluates to a value. It can be a constant, a reference to something holding a value, or a computation over other expressions. Because its result is a value, an expression can be nested inside another expression. section 11.0 section 11.6

Expressions capture detailed business logic: calculate values from input or other values, test conditions, and specify outcomes from conditions and calculations. An attribute’s optional initial value is an expression that must produce a value of that attribute’s type. section 11.0 section 1.2.1

Expression concepts provide the building blocks:

  • Number produces a literal numeric value. section 8.4.1
  • Attribute Reference produces the value held by a referenced attribute. section 8.4.1
  • Binary Operation combines left and right operand expressions with one binary operator. section 11.6

Arity—the number of contained subexpressions or operands—is the main characteristic of an expression concept. Concepts with arity greater than zero compose recursively: evaluated subexpressions supply values to larger expressions, allowing a small set of concepts to form arbitrarily large ASTs. section 11.5.2 section 11.6

Most operation-oriented expression concepts have one operator. For a Binary Operation, that operator governs the generated arithmetic expression; binary operators include arithmetic, logical, and comparison operators. Literal values are an obvious exception because they need not operate on subexpressions. section 11.5.3 section 11.6

Computed rental price

A Rental attribute can compute rental price after discount from rental price before discount and discount. The generated runtime represents it as a JavaScript getter, so the property is read-only and the form displays its changing computed value rather than accepting input: `this.rentalPriceBeforeDiscount - this.discount * 0.01 * this.rentalPriceBeforeDiscount`. section 11.3 section 11.0

Figure 11.1 The relation between the values of the four attributes of the Rental record type
figure

Inspect the Rental attribute relationship that motivates an expression for a computed, non-editable price. section 11.0

How Building User-Friendly DSLs covers it

How deep it goes
Defines expressions, develops binary-operation ASTs and runtime generation, and defers order of operations to later treatment.
Examples
Uses Rental price-after-discount calculation, Number values, Attribute References, generated JavaScript getters, and a read-only form field.
What it assumes
Introduces expressions through familiar arithmetic while assuming enough JavaScript to use generated getters.
What you can do after
Build it from scratch by adding expression concepts and generating computed runtime values from their ASTs.
How it connects
Connects expressions to ASTs, Binary Operation, Attribute Reference, Number, operators, arity, compositionality, and projectional DSLs.

Also explained in

Compare how 11 books explain expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.