Type computation determines the type an expression would produce. In a DSL, it supports type safety by computing types for subexpressions and combining them with operators, references, and context. section 13.4
A practical implementation follows the expression AST from its leaves upward:
- Read an attribute reference’s declared type. section 13.1
- Compute each child expression, then combine the resulting types according to the operator. section 13.1
- Return a best-effort type when possible; otherwise return the special untype value so errors do not obscure the original problem. section 13.4
- Use ancestors when the type depends on surrounding context; this is type inference rather than purely local computation. section 13.4 section 13.1
Rental price expression
For a rental-price expression such as rental price − discount of rental price, rental price has type amount and discount has type percentage. The of operation produces amount, and subtracting two amounts produces amount. A recursive typeOf function can implement the same calculation directly on the expression AST. section 13.1
