Error Lab
Every diagnostic below is the real output of ptc, not a mock-up. Pick a mistake, read how the compiler teaches the canonical form, then flip to the fix.
A field of the element, read on the list
toml.parse returns the tables as a List — a field read on the list itself is the first error, and a guessed field name on the element would be the second, invisible until the first is fixed. The error lists the ELEMENT's real fields and the way to one, so both are answered in one round (found by Baseline 11).
import toml
fn root_keys(text: Text) -> Result[Int, Text]:
let doc = toml.parse(text)?
return Ok(doc.entries.len())
fn main() -> Void:
match root_keys("name = \"demo\"\nport = 8080\n"):
case Ok(n):
print("{n}")
case Err(e):
print(e)The compiler answers
error: values of type List[toml.Table] have no fields — the ELEMENT type toml.Table has: name, pairs; pick one first (xs[0], or 'match xs.first(): case Some(x):') or loop 'for x in xs:' (line 5, column 19)import toml
fn root_keys(text: Text) -> Result[Int, Text]:
let tables = toml.parse(text)?
for table in tables:
if table.name == "":
return Ok(table.pairs.len())
return Ok(0)
fn main() -> Void:
match root_keys("name = \"demo\"\nport = 8080\n"):
case Ok(n):
print("{n}")
case Err(e):
print(e)Fixed — and it runs
2