diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-08-10 16:13:29 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-08-10 16:13:29 -0400 |
| commit | b64ec73b036ac94688af06f99f8cddbecc8798c8 (patch) | |
| tree | b55855c49216ee6730a5492ae2a8e146c36f8fa5 | |
| parent | b37bd70b602b7ac6427dcf29f7cd9241b0a7ae09 (diff) | |
Update learnxiny to use new table syntax
| -rw-r--r-- | learnxiny.tm | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/learnxiny.tm b/learnxiny.tm index fdc24516..ff0285a1 100644 --- a/learnxiny.tm +++ b/learnxiny.tm @@ -89,9 +89,16 @@ func main(): // Tables are efficient hash maps table := {"one": 1, "two": 2} - >> table["two"] + >> table:get("two") = 2 + // You can supply a default argument in case a key isn't found: + >> table:get("xxx", default=0) + = 0 + + // Otherwise, a runtime error will be raised: + // >> table:get("xxx") + // Tables can be iterated over either by key or key,value: for key in table: pass @@ -106,14 +113,12 @@ func main(): >> table.values = [1, 2] - // Tables can have default values and fallbacks: - table2 := {"three": 3; fallback=table; default=0} - >> table2["two"] + // Tables can have fallbacks: + table2 := {"three": 3; fallback=table} + >> table2:get("two") = 2 - >> table2["three"] + >> table2:get("three") = 3 - >> table2["???"] - = 0 // If no default is provided and a missing key is looked up, the program // will print an error message and halt. @@ -239,7 +244,7 @@ func demo_structs(): = yes table := {alice: "first", bob: "second"} - >> table[alice] + >> table:get(alice) = "first" |
