liveBook · Manning

Data type

As explained in Building User-Friendly DSLs

A specification of the kind of value or relation that a property can store.

A data type specifies what values a property may store and what those values mean. In a domain-specific language, every concept property has a type; property types also govern which instances can be related through settings. A record type, for example, groups attributes that belong to the same record. section 1.4.2 section 1.5

Types distinguish values that may share a low-level representation but carry different domain meaning. Amount and percentage attributes can both hold numbers, yet $3.0, 3.0%, and 300% are not interchangeable. A date range represents a different kind of value altogether, such as a period between two dates. section 13.0

A type system makes types useful by performing three linked jobs:

  • Represent ranges of domain-specific values as type objects. section 13.0
  • Compute a type for each expression from its contents and subexpressions. section 13.0
  • Compare computed types in constraints and report type errors that help domain experts repair invalid expressions. section 13.0

Formatting an initial numeric value

A projection can inspect the containing Data Attribute’s type before rendering a Number. When the type is amount, it displays a $ prefix; when it is percentage, it displays a % postfix. The renderer receives the parent AST object because the Number alone does not carry that type information. section 4.2.3

Type changes are structural changes. Adding a property or concept widens the set of valid ASTs, while changing a property to an incompatible type, removing it, or refactoring related concepts can require an AST migration to make existing content conform again. section 10.3.2

Figure 13.1 An expression that doesn’t make sense due to the types of its subexpressions
figure

Inspect an expression whose subexpressions have incompatible types, motivating computed expression types and type-error reporting. section 13.0

How Building User-Friendly DSLs covers it

How deep it goes
Develops domain value types into computed expression types and checking constraints, briefly deferring compatibility beyond equality.
Examples
Uses car-rental amount, percentage, and date-range attributes, plus a Number projection rendered with $ or %.
What you can do after
Build it from scratch by representing types, computing expression types, and comparing them in checking constraints.
How it connects
Connects properties, concepts, settings, relations, ASTs, projections, expressions, constraints, and Runtime code generation.

Also explained in

Compare how 18 books explain data type →

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.