Skip to content

Typed literals

A scalar (number | string | boolean | null) that sits in a child-kind slot is held as a real node, so a slot always holds a node. The wrapper is a concept of the Literal family, chosen by the value's type:

Value Head
true / false BooleanLiteral
a whole number IntLiteral
a number with a fraction FloatLiteral
null NoneLiteral
a string, or anything else Literal

All five are engine-level (src/concepts-literal.tsx, resolved by Language#getConcept under every language, never registered by a plugin). The four typed ones are subclasses of Literal, so value, eval(), the editor projection, the Python/JavaScript spelling and the type inference are shared; they differ in head, description and (for FloatLiteral) printing a whole number as 2.0 in Python.

Rules

  • Never compare a head to LITERAL_HEAD. Ask isLiteralHead(head) (src/ast.ts) for "is this a wrapper", unwrapLiteral(x) for the value, literalHeadFor(value) for the head a value gets. tests/test-no-language-literals.mjs still counts hardcoded heads.
  • The wrapper is chosen in one place: wrapForSlot (src/base-concept.ts) at construction, and the expression-statement normalizer for a raw scalar in a statement slot. Prop/Ref slots never wrap; a parser still returns bare primitives and lets the slot decide.
  • A value edit may change the type (5 edited to 5.5, a boolean cleared to text). normalizeLiteralHeads (src/model.ts, called from normalizeExpressionStatements, so on every fire()/setRoot()) replaces the wrapper by one of the right type and keeps its id. A FloatLiteral holding a whole number is allowed to stay; a generic Literal holding a number, boolean or null (old-shape data arriving from a clipboard, a peer or an undo stash) is upgraded to its typed wrapper the same way.
  • Python 2.0 is parsed as the number 2, so it reads back as IntLiteral(2) and prints 2 (as before); only a FloatLiteral created as such prints 2.0.
  • LionWeb keeps one Literal classifier with a kind/value property pair on the wire; import picks the head with literalHeadFor.