liveBook · Manning

Type equality

As explained in Building User-Friendly DSLs

A type equality is a constraint asserting that two type expressions must denote the same type.

Type equality asserts that two type expressions denote the same type. When types are represented as objects, compare their stable IDs rather than JavaScript object identity. section 13.3

An implementation can use type equality to:

  • Define areEqual(actual, expected) as actual.id === expected.id. section 13.3
  • Check operator rules, such as requiring the right operand of “of” to equal the built-in amount type. section 13.3
  • Validate that an attribute’s declared type equals the type inferred for its value, then report both rendered types when they differ. section 13.3
  • Select presentation details such as dollar, days, or percent markers by comparing a value’s type with built-in types. section 13.3

Comparing an attribute and its value

If an attribute declares amount but its value infers as percentage, areEqual returns false. Constraint checking can then report that the attribute and value types must match, rendering the two types for the diagnostic. section 13.3

Table 13.1 The type objects for built-in types
Type name
ID
Concept label
Settings
amount
numberType_amount
Number Type
{ "unit": "$" }
date range
numberType_dateRange
Interval Type
{ "unit": "day" }
percentage
numberType_percentage
Number Type
{ "unit": "%" }
untype
untype_untype
Untype
{}

Built-in type objects make equality concrete: amount, date range, percentage, and untype each have distinct IDs, concepts, and settings. section 13.3

This equality-based notion of matching is deliberately simple. Type compatibility can instead use directional assignability, including relationships involving inheritance or type coercion. MPS can produce type equalities for algorithmic inference. section 13.3 section 15.4.2

How Building User-Friendly DSLs covers it

How deep it goes
Defines ID-based comparison and applies it in typing code, while stopping short of general assignability semantics.
Examples
Uses built-in amount, date range, percentage, and untype objects plus attribute-value checks and number-unit rendering.
What it assumes
Assumes AST-like objects and type computation, while establishing object-based type identity and equality explicitly.
What you can do after
Build it from scratch by defining stable type IDs, implementing areEqual, and applying it to typing and constraints.
How it connects
Connects type compatibility, assignability, inheritance, type coercion, constraint checking, and type inference.

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.