liveBook · Manning

Best-effort type

As explained in Building User-Friendly DSLs

A provisional type produced when the obviously correct type of an expression cannot be determined but a sensible approximation is available.

A best-effort type is a sensible approximation returned when an expression has no obviously correct type. It keeps type computation running while a type-checking constraint reports the error. section 13.1 section 13.4

Compute it as though a minimal modification made the expression meaningful, often by effectively ignoring one operand’s type. If no sensible approximation exists, return the special untype value. section 13.1 section 13.4

Typical binary-operation rules include:

  • Treat subtraction as the left numeric type when the left operand is amount or percentage. section 13.1
  • When subtraction has an untyped left operand and a numeric right operand, use the right operand’s type. section 13.1
  • Treat an “of” expression with an amount-typed right operand as amount; otherwise return untype. section 13.1
  • Mark a best-effort result for an error while leaving impossible combinations as x until no approximation exists. section 13.1

Rental price expression

For an amount-typed right operand, “<left operand> of <amount-typed right operand>” can produce amount even when the left operand is invalid. The type checker still reports an error on that expression. section 13.1

How Building User-Friendly DSLs covers it

How deep it goes
Defines fallback rules for binary operations and defers exact error messages until later type-checking work.
Examples
Uses amount and percentage arithmetic, “of” expressions, rental pricing, and a table-marking exercise with x and ! symbols.
What it assumes
Assumes expressions, subexpressions, and computed types, while introducing untype and the separation between computation and checking.
What you can do after
Diagnose invalid expressions without allowing type mismatches to obscure their root causes.
How it connects
Connects best-effort types with type computation, type checking constraints, type inference, AST objects, and untype.

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.