liveBook · Manning

Operator

As explained in Building User-Friendly DSLs

A language element that governs the operation performed by an expression concept on its contained subexpressions.

An operator is a language element that determines the operation performed by an expression on its contained subexpressions. It commonly works with operands and gives the expression its operational meaning. A binary operation, for example, combines a left operand, an operator, and a right operand; “discount of rental price before discount” can calculate 20% of $60 as $12. section 11.1 section 11.5.3

Operators are classified by placement and operand count:

  • Infix operators appear between operands, as in 1 / 2; binary operations use this placement. section 11.1
  • Prefix operators appear before their operands, as with logical negation (! false) and arithmetic negation (- 42). section 11.1
  • Postfix operators appear after their operands, as with increment and decrement operations such as i++ and j--. section 11.1
  • Common binary operators include arithmetic (*, /, +, -), logical (&&, ||), and comparison (<, ≤, =, ≠, ≥, >) operators. section 11.6

Discount calculation

Represent “discount of rental price before discount” as a binary operation: the left operand evaluates to the discount value, the “of” operator determines the calculation, and the right operand evaluates to the rental price before discount. With values of 20% and $60, the result is $12. section 11.1

Figure 11.2 The expression for calculating the value of rental price after discount from the values of discount and rental price before discount
figure

Trace the rental-price expression used to calculate the value after discount from the discount and pre-discount rental-price values. section 11.1

How Building User-Friendly DSLs covers it

How deep it goes
Explains operator placement, operands, arity, and refactoring to one operator per expression, but defers order of operations.
Examples
Uses rental discounts, arithmetic, logical, comparison, negation, division, and increment or decrement examples.
What it assumes
Assumes familiarity with expressions and subexpressions while defining operands, notation, operator placement, and arity in context.
What you can do after
Evaluate/understand operator notation, operand structure, and the role of operators in composable expression concepts.
How it connects
Connects operators with expressions, operands, binary operations, abstract syntax trees, arity, and composition.

Also explained in

Compare how 7 books explain operator →

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.