ErrorsValues and types · rule
type.map_literal_key
A map literal's key is a variable, a parameter, a constant or a call, when each key is written out as a literal or an enum variant.
Area 2, Values and types; not declinable; the next step is usually the agent's.
Why Jadpo refuses it
A map literal, ["rye": 450], is a fixed table written in the code, so
each of its keys is written out: a literal of the key type, such as
"rye", or an enum variant, such as Region.islands. Every key is then
known while compiling, and two entries for one key are an error rather
than one silently replacing the other when the code runs. A name as a key
would also read two ways: [loaf: 3] looks like the text "loaf".
What it looks like
A parameter as a key:
type Loaf = Text { min_length: 1}type Stock = Int { min: 0}function first_delivery(loaf: Loaf) -> Map<Loaf, Stock> { return ["rye": 12, loaf: 3]}
error: `loaf` can't be a map literal's key --> main.jadpo:10:24 |10 | return ["rye": 12, loaf: 3] | ^^^^ not a literal or a variant reason: A map literal's keys are written out, each a literal of the key type or an enum variant, so every key is known while compiling and none can repeat another. Written as a key, `loaf` could be read as the text `"loaf"`. next: Add this entry with `map.with` instead (agent) fix: 10 - return ["rye": 12, loaf: 3] 10 + return map.with(["rye": 12], loaf, 3) rule: type.map_literal_key help: https://jadpo.dev/docs/errors/type.map_literal_keycheck failed: 1 error, 1 filenext: jadpo fix fixes this error for you, then checks again
(agent) at the end of the next: line means your coding agent can make
the fix itself.
How to fix it
If the key is only known while the code runs, start from the entries
that are written out and add it with map.with, which is what the edit
writes:
type Loaf = Text { min_length: 1}type Stock = Int { min: 0}function first_delivery(loaf: Loaf) -> Map<Loaf, Stock> { return map.with(["rye": 12], loaf, 3)}
map.with replaces an entry whose key is already there, so when loaf is
"rye" the delivery holds 3 of it.
If the key is always the same value, write that value out:
type Loaf = Text { min_length: 1}type Stock = Int { min: 0}function first_delivery() -> Map<Loaf, Stock> { return ["rye": 12, "spelt": 3]}
first_delivery now takes no loaf, and always holds 12 rye and 3 spelt.
Related
Markdown for agents: /docs/errors/type.map_literal_key.md.Built from Jadpo's main branch at commit ee389ee.