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. AskisLiteralHead(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.mjsstill 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/Refslots never wrap; a parser still returns bare primitives and lets the slot decide. - A value edit may change the type (
5edited to5.5, a boolean cleared to text).normalizeLiteralHeads(src/model.ts, called fromnormalizeExpressionStatements, so on everyfire()/setRoot()) replaces the wrapper by one of the right type and keeps its id. AFloatLiteralholding a whole number is allowed to stay; a genericLiteralholding 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.0is parsed as the number 2, so it reads back asIntLiteral(2)and prints2(as before); only aFloatLiteralcreated as such prints2.0. - LionWeb keeps one
Literalclassifier with akind/valueproperty pair on the wire; import picks the head withliteralHeadFor.