From 51f3257dd35b21344e4f55bbc8548a6bd8562305 Mon Sep 17 00:00:00 2001 From: Alexander Satretdinov Date: Mon, 16 Nov 2026 22:08:20 +0200 Subject: [PATCH] no objects: a struct is a record and a map is a dictionary; map literal [k: v] and [:]; json.parse gives maps, spread into a struct for fields; options are structs; record patterns take structs, rest is a map; struct fields only by name, no s["f"] --- README.md | 2 +- docs/src/SUMMARY.md | 1 - docs/src/data/arrays.md | 8 +- docs/src/data/enums.md | 8 +- docs/src/data/map-and-set.md | 73 +++--- docs/src/data/methods.md | 28 ++- docs/src/data/structs.md | 42 ++-- docs/src/errors-and-exit-codes.md | 14 +- docs/src/internals/performance.md | 2 +- docs/src/introduction.md | 10 +- docs/src/language/conditionals.md | 13 +- docs/src/language/destructuring.md | 114 +++++----- docs/src/language/error-handling.md | 20 +- docs/src/language/functions.md | 44 ++-- docs/src/language/loops.md | 25 +- docs/src/language/nil-safety.md | 44 ++-- docs/src/language/pattern-matching.md | 82 ++++--- docs/src/language/spread-and-rest.md | 45 +--- docs/src/language/strings.md | 10 +- docs/src/language/values.md | 100 ++++---- docs/src/language/variables.md | 22 +- docs/src/programs/examples.md | 2 +- docs/src/programs/modules.md | 4 +- docs/src/stdlib/array.md | 44 ++-- docs/src/stdlib/builtins.md | 35 ++- docs/src/stdlib/fs.md | 6 +- docs/src/stdlib/json-http.md | 53 ++--- docs/src/stdlib/object.md | 110 --------- docs/src/stdlib/os.md | 15 +- docs/src/stdlib/regex.md | 8 +- docs/src/stdlib/time.md | 8 +- examples/blog/md/block.rn | 25 +- examples/blog/site/build.rn | 18 +- examples/blog/site/frontmatter.rn | 8 +- examples/blog/site/template.rn | 2 +- examples/calc.rn | 45 ++-- examples/chess.rn | 32 ++- examples/kv/main.rn | 12 +- examples/kv/store.rn | 53 +++-- examples/linked-list.rn | 4 +- examples/log-report.rn | 40 ++-- examples/query/aggregate.rn | 2 +- examples/query/expr.rn | 16 +- examples/query/output.rn | 2 +- examples/query/parser.rn | 22 +- examples/query/plan.rn | 2 +- examples/query/table.rn | 2 +- examples/shop.rn | 64 +++--- examples/todo.rn | 30 +-- examples/validate.rn | 215 ++++++++++-------- extras/nvim/syntax/rune.vim | 2 +- pkg/ast/expression.go | 55 +---- pkg/ast/pattern.go | 12 +- pkg/ast/statement.go | 6 +- pkg/ast/token.go | 3 - pkg/format/format.go | 4 +- pkg/helpers/assert.go | 16 +- pkg/helpers/format.go | 60 ++--- pkg/rune/bench_test.go | 26 +-- pkg/rune/bytecode.go | 20 +- pkg/rune/compile.go | 10 +- pkg/rune/compile_expr.go | 56 ++--- pkg/rune/compile_pattern.go | 10 +- pkg/rune/hints.go | 8 +- pkg/rune/http_test.go | 22 +- pkg/rune/in.go | 10 +- pkg/rune/iter.go | 35 +-- pkg/rune/methods.go | 26 +-- pkg/rune/moves.go | 9 +- pkg/rune/ops.go | 75 +----- pkg/rune/parse_expr.go | 181 +++++---------- pkg/rune/parse_pattern.go | 49 ++-- pkg/rune/parse_stmt.go | 4 - pkg/rune/report_test.go | 2 +- pkg/rune/resolve_const.go | 8 +- pkg/rune/resolve_impl.go | 2 +- pkg/rune/resolve_pattern.go | 6 +- pkg/rune/resolve_struct.go | 22 +- pkg/rune/resolver.go | 38 +--- pkg/rune/vm.go | 77 ++----- pkg/rune/vm_slow.go | 96 +++----- pkg/rune/vm_test.go | 2 +- pkg/stdlib/collections.go | 74 ++---- pkg/stdlib/convert.go | 87 ++++--- pkg/stdlib/core.go | 4 +- pkg/stdlib/http.go | 34 ++- pkg/stdlib/json.go | 37 ++- pkg/stdlib/native.go | 2 - pkg/stdlib/objects.go | 47 ---- pkg/stdlib/process.go | 41 +--- pkg/stdlib/regex.go | 6 +- pkg/stdlib/time.go | 7 +- pkg/value/hashmap.go | 2 +- pkg/value/object.go | 161 ------------- pkg/value/shared_test.go | 1 - pkg/value/value.go | 65 ++---- test/arrays/accessing_primitives.rn | 2 +- test/arrays/accessing_primitives.stderr | 4 +- test/arrow/block.rn | 4 +- test/arrow/destructure.rn | 3 +- test/arrow/object_body.rn | 4 - test/arrow/object_body.stderr | 26 --- test/arrow/object_parens.rn | 1 - test/bytes/buffer.rn | 2 +- test/catch/clauses.rn | 2 +- test/catch/construct.rn | 2 +- test/catch/kinds.rn | 2 +- test/compound_assignment/index.rn | 6 +- test/compound_assignment/missing_property.rn | 4 +- .../missing_property.stderr | 10 +- test/const/array_errors.rn | 4 +- test/const/array_errors.stderr | 12 +- test/const/map_errors.rn | 8 + test/const/map_errors.stderr | 36 +++ test/const/maps.rn | 33 +++ test/const/match.rn | 3 +- test/const/not_constant.rn | 6 +- test/const/not_constant.stderr | 16 +- test/const/object_errors.rn | 10 - test/const/object_errors.stderr | 52 ----- test/const/objects.rn | 34 --- test/default/basic.rn | 6 +- test/destructure/closure.rn | 8 +- test/destructure/defaults.rn | 21 +- test/destructure/defaults_errors.rn | 2 +- test/destructure/defaults_errors.stderr | 2 +- ...s_not_object.rn => defaults_not_struct.rn} | 0 ...ject.stderr => defaults_not_struct.stderr} | 2 +- test/destructure/defaults_own.rn | 2 +- test/destructure/defaults_own.stderr | 2 +- test/destructure/empty.rn | 2 +- test/destructure/empty.stderr | 4 +- test/destructure/evaluation_order.rn | 8 +- test/destructure/immutable.rn | 3 +- test/destructure/immutable.stderr | 16 +- test/destructure/let_mut.rn | 3 +- test/destructure/let_mut_pattern.rn | 2 +- test/destructure/let_mut_pattern.stderr | 2 +- test/destructure/map.rn | 1 + test/destructure/map.stderr | 7 + test/destructure/missing_property.rn | 3 +- test/destructure/missing_property.stderr | 7 +- test/destructure/nested.rn | 25 +- test/destructure/nested_missing.rn | 4 +- test/destructure/nested_missing.stderr | 7 +- .../{not_object.rn => not_struct.rn} | 0 .../{not_object.stderr => not_struct.stderr} | 2 +- test/destructure/object.rn | 13 -- test/destructure/object_rest.rn | 54 ----- test/destructure/object_rest_default.rn | 1 - test/destructure/object_rest_default.stderr | 5 - test/destructure/object_rest_not_last.rn | 1 - test/destructure/object_rest_not_last.stderr | 5 - .../destructure/object_rest_not_object.stderr | 5 - test/destructure/own_initializer.rn | 3 +- test/destructure/own_initializer.stderr | 8 +- test/destructure/parameters.rn | 10 +- test/destructure/record.rn | 16 ++ test/destructure/record_rest.rn | 53 +++++ test/destructure/record_rest_default.rn | 1 + test/destructure/record_rest_default.stderr | 5 + test/destructure/record_rest_not_last.rn | 1 + test/destructure/record_rest_not_last.stderr | 5 + ...ot_object.rn => record_rest_not_struct.rn} | 2 +- .../destructure/record_rest_not_struct.stderr | 5 + test/destructure/redeclare.rn | 5 +- test/destructure/redeclare.stderr | 14 +- test/destructure/refutable.rn | 2 +- test/destructure/refutable.stderr | 4 +- test/destructure/string_key.rn | 1 - test/destructure/string_key.stderr | 5 - test/destructure/struct.rn | 3 +- test/discard/let_and_params.rn | 4 +- test/discard/read.rn | 1 - test/discard/read.stderr | 7 - test/enum/convert.rn | 4 +- test/enum/fields.rn | 4 +- test/for_in/bare_body.rn | 2 +- test/for_in/{object.rn => map.rn} | 2 +- test/for_in/mut.rn | 16 +- test/for_in/mut_kind.rn | 2 +- test/for_in/mut_kind.stderr | 4 +- test/for_in/not_iterable.rn | 2 +- test/for_in/not_iterable.stderr | 4 +- test/for_in/pattern.rn | 5 +- test/for_in/pattern_missing.rn | 3 +- test/for_in/pattern_missing.stderr | 7 +- test/for_in/reuse.rn | 2 +- test/function/tail_value.rn | 4 +- test/function_expr/in_object.rn | 5 - test/function_expr/in_object.stderr | 34 --- test/if/expression.rn | 6 +- test/if/expression_registers.rn | 4 +- test/impl/builtin_types.rn | 4 +- test/impl/field_function.rn | 1 - test/impl/mut_self.rn | 6 +- test/impl/mut_self_local.rn | 10 +- test/impl/mut_self_temporary_runtime.rn | 2 +- test/impl/mut_self_temporary_runtime.stderr | 2 +- test/impl/override_builtin.rn | 2 +- test/impl/set_fields.rn | 2 +- test/impl/set_fields_nested.rn | 8 +- test/impl/set_fields_nested.stderr | 2 +- test/import/enum_fields.rn | 2 +- test/import/module_shadow.rn | 3 +- test/import/struct_errors.rn | 2 +- test/import/struct_errors.stderr | 2 +- test/import/structs.rn | 2 +- test/in/basic.rn | 10 +- test/in/not_a_collection.rn | 2 +- test/in/not_a_collection.stderr | 4 +- test/int/stdlib.rn | 6 +- test/interpolation/basic.rn | 2 +- test/interpolation/nested.rn | 2 +- test/interpolation/runtime_error.rn | 4 +- test/interpolation/runtime_error.stderr | 10 +- test/is/binding_errors.rn | 2 +- test/is/binding_errors.stderr | 2 +- test/is/bindings.rn | 8 +- test/is/builtin_types.rn | 7 +- test/is/patterns.rn | 3 +- test/map/basics.rn | 23 +- test/map/dot_read.rn | 2 + test/map/dot_read.stderr | 8 + test/map/dot_set.rn | 2 + test/map/dot_set.stderr | 6 + test/map/duplicate_key.rn | 5 + test/map/duplicate_key.stderr | 26 +++ test/map/functions.rn | 9 + test/map/iterate_change.rn | 8 +- test/map/keys.rn | 6 +- test/map/literal.rn | 25 ++ test/map/order.rn | 24 ++ test/map/unhashable_key.rn | 1 + test/map/unhashable_key.stderr | 5 + test/map/unterminated.rn | 1 + test/map/unterminated.stderr | 5 + test/match/arm_object.rn | 6 - test/match/arrays.rn | 8 +- test/match/bindings.rn | 5 +- test/match/defaults.rn | 12 +- test/match/objects.rn | 23 -- test/match/or_binding.rn | 2 +- test/match/or_binding.stderr | 8 +- test/match/ranges.rn | 5 +- test/match/records.rn | 24 ++ test/match/structs.rn | 2 +- test/match/syntax.rn | 2 +- test/match/syntax.stderr | 6 +- test/mutability/contents.rn | 6 +- test/mutability/contents.stderr | 20 +- .../mutability/contents_before_declaration.rn | 4 +- .../contents_before_declaration.stderr | 2 +- test/mutability/for_in_mut.rn | 4 +- test/mutability/in_place_builtins.rn | 8 +- test/mutability/in_place_builtins.stderr | 10 +- test/mutability/map_set_in_place.rn | 4 +- test/mutability/mut_argument.rn | 4 +- test/mutability/mut_argument.stderr | 6 +- test/mutability/mutable_contents.rn | 6 +- test/nullish/basic.rn | 3 +- test/object/function_entries.rn | 3 - test/object/function_entries.stderr | 9 - test/object/function_index.rn | 3 - test/object/function_index.stderr | 8 - test/object/function_property.rn | 3 - test/object/function_property.stderr | 8 - test/object/function_rest.rn | 3 - test/object/function_rest.stderr | 8 - test/object/function_spread.rn | 3 - test/object/function_spread.stderr | 8 - test/objects/computed.rn | 28 --- test/objects/computed_not_string.rn | 1 - test/objects/computed_not_string.stderr | 5 - test/objects/declare.rn | 15 -- test/objects/dot_access.rn | 13 -- test/objects/dot_missing_name.rn | 2 - test/objects/dot_missing_name.stderr | 6 - test/objects/dot_undefined.rn | 2 - test/objects/dot_undefined.stderr | 6 - test/objects/duplicate_key.rn | 4 - test/objects/duplicate_key.stderr | 19 -- test/objects/evaluation_order.rn | 7 - test/objects/function_value_runtime.rn | 3 - test/objects/function_value_runtime.stderr | 8 - test/objects/methods.rn | 19 -- test/objects/order.rn | 32 --- test/objects/shorthand.rn | 11 - test/objects/shorthand_undefined.rn | 1 - test/objects/shorthand_undefined.stderr | 5 - test/objects/statement_start.rn | 2 - test/objects/statement_start.stderr | 6 - test/optional/assign.rn | 3 +- test/optional/assign.stderr | 12 +- test/optional/basic.rn | 4 +- test/optional/condition.rn | 3 +- test/optional/missing.rn | 7 +- test/optional/missing.stderr | 8 +- test/optional/missing_name.rn | 2 +- test/optional/missing_name.stderr | 2 +- .../optional/{not_object.rn => not_struct.rn} | 0 .../{not_object.stderr => not_struct.stderr} | 2 +- test/optional/short_circuit.rn | 5 +- test/optional/short_circuit.stderr | 6 +- test/os/os.rn | 16 +- test/parser_recovery/nested_braces.rn | 2 +- test/parser_recovery/nested_braces.stderr | 10 +- test/print/quoted_strings.rn | 4 +- test/property/dot_missing_name.rn | 2 + test/property/dot_missing_name.stderr | 6 + .../read_non_struct.rn} | 0 .../read_non_struct.stderr} | 2 +- .../set_non_struct.rn} | 0 .../set_non_struct.stderr} | 2 +- test/regex/regex.rn | 2 +- test/regression/container_equality.rn | 2 +- test/regression/map_string_keys.rn | 4 + test/regression/object_string_keys.rn | 4 - test/regression/print_containers.rn | 2 +- test/regression/self_copy.rn | 6 +- test/regression/truthy_values.rn | 2 +- test/regression/type_names_in_errors.rn | 2 +- test/regression/type_names_in_errors.stderr | 10 +- test/slice/not_sliceable.rn | 2 +- test/slice/not_sliceable.stderr | 4 +- test/spread/duplicate.rn | 5 +- test/spread/duplicate.stderr | 10 +- test/spread/not_array.rn | 2 +- test/spread/not_array.stderr | 4 +- test/spread/not_object.rn | 1 - test/spread/not_object.stderr | 5 - test/spread/object.rn | 14 -- test/stdlib/arrays.rn | 3 +- test/stdlib/conversions.rn | 12 +- test/stdlib/json.rn | 10 +- test/stdlib/len_method.rn | 4 +- test/stdlib/time.rn | 20 +- test/struct/builtins.rn | 16 +- test/struct/builtins_errors.rn | 4 +- test/struct/call.rn | 2 +- test/struct/call.stderr | 2 +- test/struct/convert.rn | 18 +- test/struct/convert_extra_key.rn | 2 +- test/struct/convert_extra_key.stderr | 2 +- test/struct/convert_missing_field.rn | 2 +- test/struct/convert_missing_field.stderr | 2 +- test/struct/default_not_literal.rn | 6 +- test/struct/default_not_literal.stderr | 14 +- test/struct/default_own.rn | 14 +- test/struct/dynamic_key.rn | 8 - test/struct/equality.rn | 4 +- test/struct/for_in.rn | 4 +- test/struct/for_in.stderr | 4 +- test/struct/index.rn | 8 + test/struct/index.stderr | 6 + test/struct/is.rn | 2 +- test/struct/literal.rn | 1 - test/struct/nested.rn | 4 +- test/struct/number_index.rn | 4 - test/struct/number_index.stderr | 6 - test/struct/set_element_field.rn | 6 +- test/struct/set_index.rn | 4 +- test/struct/set_index.stderr | 15 +- test/struct/set_index_missing.rn | 8 - test/struct/set_index_missing.stderr | 9 - test/struct/spread.rn | 4 +- test/struct/unknown_index_read.rn | 4 - test/struct/unknown_index_read.stderr | 6 - test/throw/expression.rn | 4 +- test/truthiness/empty.rn | 6 +- test/try/catch_native_error.rn | 4 +- test/try/expr.rn | 4 +- test/value/arrays.rn | 22 +- test/value/containers.rn | 150 ++++++------ test/value/inout.rn | 12 +- test/value/moves.rn | 3 +- test/value/natives.rn | 31 +-- test/value/places.rn | 44 ++-- test/value/structs.rn | 20 +- test/vm/loop_closures.rn | 2 +- test/vm/registers.rn | 6 +- testdata/disasm/data.rn | 6 +- testdata/disasm/methods.golden | 4 +- testdata/disasm/spread.golden | 16 +- testdata/disasm/spread.rn | 2 +- testdata/disasm/struct.golden | 16 +- testdata/disasm/struct.rn | 2 +- testdata/fmt/layout.golden | 6 +- testdata/fmt/layout.rn | 6 +- 389 files changed, 2204 insertions(+), 3162 deletions(-) delete mode 100644 docs/src/stdlib/object.md delete mode 100644 pkg/stdlib/objects.go delete mode 100644 pkg/value/object.go delete mode 100644 test/arrow/object_body.rn delete mode 100644 test/arrow/object_body.stderr delete mode 100644 test/arrow/object_parens.rn create mode 100644 test/const/map_errors.rn create mode 100644 test/const/map_errors.stderr create mode 100644 test/const/maps.rn delete mode 100644 test/const/object_errors.rn delete mode 100644 test/const/object_errors.stderr delete mode 100644 test/const/objects.rn rename test/destructure/{defaults_not_object.rn => defaults_not_struct.rn} (100%) rename test/destructure/{defaults_not_object.stderr => defaults_not_struct.stderr} (84%) create mode 100644 test/destructure/map.rn create mode 100644 test/destructure/map.stderr rename test/destructure/{not_object.rn => not_struct.rn} (100%) rename test/destructure/{not_object.stderr => not_struct.stderr} (87%) delete mode 100644 test/destructure/object.rn delete mode 100644 test/destructure/object_rest.rn delete mode 100644 test/destructure/object_rest_default.rn delete mode 100644 test/destructure/object_rest_default.stderr delete mode 100644 test/destructure/object_rest_not_last.rn delete mode 100644 test/destructure/object_rest_not_last.stderr delete mode 100644 test/destructure/object_rest_not_object.stderr create mode 100644 test/destructure/record.rn create mode 100644 test/destructure/record_rest.rn create mode 100644 test/destructure/record_rest_default.rn create mode 100644 test/destructure/record_rest_default.stderr create mode 100644 test/destructure/record_rest_not_last.rn create mode 100644 test/destructure/record_rest_not_last.stderr rename test/destructure/{object_rest_not_object.rn => record_rest_not_struct.rn} (61%) create mode 100644 test/destructure/record_rest_not_struct.stderr delete mode 100644 test/destructure/string_key.rn delete mode 100644 test/destructure/string_key.stderr rename test/for_in/{object.rn => map.rn} (84%) delete mode 100644 test/function_expr/in_object.rn delete mode 100644 test/function_expr/in_object.stderr create mode 100644 test/map/dot_read.rn create mode 100644 test/map/dot_read.stderr create mode 100644 test/map/dot_set.rn create mode 100644 test/map/dot_set.stderr create mode 100644 test/map/duplicate_key.rn create mode 100644 test/map/duplicate_key.stderr create mode 100644 test/map/functions.rn create mode 100644 test/map/literal.rn create mode 100644 test/map/order.rn create mode 100644 test/map/unhashable_key.rn create mode 100644 test/map/unhashable_key.stderr create mode 100644 test/map/unterminated.rn create mode 100644 test/map/unterminated.stderr delete mode 100644 test/match/arm_object.rn delete mode 100644 test/match/objects.rn create mode 100644 test/match/records.rn delete mode 100644 test/object/function_entries.rn delete mode 100644 test/object/function_entries.stderr delete mode 100644 test/object/function_index.rn delete mode 100644 test/object/function_index.stderr delete mode 100644 test/object/function_property.rn delete mode 100644 test/object/function_property.stderr delete mode 100644 test/object/function_rest.rn delete mode 100644 test/object/function_rest.stderr delete mode 100644 test/object/function_spread.rn delete mode 100644 test/object/function_spread.stderr delete mode 100644 test/objects/computed.rn delete mode 100644 test/objects/computed_not_string.rn delete mode 100644 test/objects/computed_not_string.stderr delete mode 100644 test/objects/declare.rn delete mode 100644 test/objects/dot_access.rn delete mode 100644 test/objects/dot_missing_name.rn delete mode 100644 test/objects/dot_missing_name.stderr delete mode 100644 test/objects/dot_undefined.rn delete mode 100644 test/objects/dot_undefined.stderr delete mode 100644 test/objects/duplicate_key.rn delete mode 100644 test/objects/duplicate_key.stderr delete mode 100644 test/objects/evaluation_order.rn delete mode 100644 test/objects/function_value_runtime.rn delete mode 100644 test/objects/function_value_runtime.stderr delete mode 100644 test/objects/methods.rn delete mode 100644 test/objects/order.rn delete mode 100644 test/objects/shorthand.rn delete mode 100644 test/objects/shorthand_undefined.rn delete mode 100644 test/objects/shorthand_undefined.stderr delete mode 100644 test/objects/statement_start.rn delete mode 100644 test/objects/statement_start.stderr rename test/optional/{not_object.rn => not_struct.rn} (100%) rename test/optional/{not_object.stderr => not_struct.stderr} (88%) create mode 100644 test/property/dot_missing_name.rn create mode 100644 test/property/dot_missing_name.stderr rename test/{objects/property_of_non_object.rn => property/read_non_struct.rn} (100%) rename test/{objects/property_of_non_object.stderr => property/read_non_struct.stderr} (84%) rename test/{objects/set_property_of_non_object.rn => property/set_non_struct.rn} (100%) rename test/{objects/set_property_of_non_object.stderr => property/set_non_struct.stderr} (82%) create mode 100644 test/regression/map_string_keys.rn delete mode 100644 test/regression/object_string_keys.rn delete mode 100644 test/spread/not_object.rn delete mode 100644 test/spread/not_object.stderr delete mode 100644 test/spread/object.rn delete mode 100644 test/struct/dynamic_key.rn create mode 100644 test/struct/index.rn create mode 100644 test/struct/index.stderr delete mode 100644 test/struct/number_index.rn delete mode 100644 test/struct/number_index.stderr delete mode 100644 test/struct/set_index_missing.rn delete mode 100644 test/struct/set_index_missing.stderr delete mode 100644 test/struct/unknown_index_read.rn delete mode 100644 test/struct/unknown_index_read.stderr diff --git a/README.md b/README.md index a15c1f72..09a30aba 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ skip: int() cannot parse "x" as an integer. ## Features - immutable by default (`let` / `let mut`), compile-time `const` -- value semantics: arrays, objects, maps, sets and structs copy lazily, `mut` parameters are inout +- value semantics: arrays, maps, sets and structs copy lazily, `mut` parameters are inout - structs and enums with fields, `impl` methods - blocks give their last expression: `fun sq(x) { x * x }`, `if c { a } else { b }`, block match arms - `match` on literals, ranges (`200..300`, `500..`), types, structs, arrays and enums, exhaustive over enum variants; the same nested patterns in `let`, parameters and `if x is { status: 200, body }`; spread, rest diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 420944e1..55e68b9a 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -39,7 +39,6 @@ - [Built-in Functions](stdlib/builtins.md) - [Array Methods](stdlib/array.md) -- [Object Methods](stdlib/object.md) - [String Methods](stdlib/string.md) - [regex](stdlib/regex.md) - [math](stdlib/math.md) diff --git a/docs/src/data/arrays.md b/docs/src/data/arrays.md index 0e9bbca5..abef24c7 100644 --- a/docs/src/data/arrays.md +++ b/docs/src/data/arrays.md @@ -195,10 +195,12 @@ chains of them read like a pipeline. All of them are in [Array Methods](../stdlib/array.md). ```rune +struct Order { id, total } + let orders = [ - { id: 1, total: 30 }, - { id: 2, total: 120 }, - { id: 3, total: 75 }, + Order { id: 1, total: 30 }, + Order { id: 2, total: 120 }, + Order { id: 3, total: 75 }, ]; let big = orders.filter(o => o.total > 50).map(o => o.id); diff --git a/docs/src/data/enums.md b/docs/src/data/enums.md index 25b3d1b8..73a812dd 100644 --- a/docs/src/data/enums.md +++ b/docs/src/data/enums.md @@ -124,11 +124,11 @@ elements. ```rune enum Dir { North, East, South, West } -let saved = json.stringify({ facing: Dir.East }); -print(saved); // {"facing":"East"} -print(Dir(json.parse(saved).facing)); // Dir.East +let saved = json.stringify(["facing": Dir.East]); +print(saved); // {"facing":"East"} +print(Dir(json.parse(saved)["facing"])); // Dir.East -let visits = map([[Dir.North, 2], [Dir.West, 1]]); +let visits = [Dir.North: 2, Dir.West: 1]; print(visits[Dir.North]); // 2 ``` diff --git a/docs/src/data/map-and-set.md b/docs/src/data/map-and-set.md index 522f1a04..42b08ebb 100644 --- a/docs/src/data/map-and-set.md +++ b/docs/src/data/map-and-set.md @@ -2,18 +2,13 @@ A **map** stores values by key; a **set** stores unique values. Use a map to count, group or index things, and a set to remember what you have seen. - -Both differ from an object in two ways: their keys may be any hashable -value, not just strings, and they are built up at run time rather than -written out field by field. Like an object, they keep keys in the order -they were first added. +Both keep keys in the order they were first added. ```rune -let mut stock = map(); -stock["apple"] = 3; -stock["pear"] = 0; +let mut stock = ["apple": 3, "pear": 0]; +stock["plum"] = 7; print(stock["apple"]); // 3 -print(stock); // map {"apple": 3, "pear": 0} +print(stock); // ["apple": 3, "pear": 0, "plum": 7] let mut visitors = set(); visitors.add("ann"); @@ -28,23 +23,31 @@ A classic use: counting words. ```rune let words = "the cat saw the dog and the cat".split(" "); -let mut counts = map(); +let mut counts = [:]; for w in words { counts[w] = (counts?.[w] ?? 0) + 1; } -print(counts); // map {"the": 3, "cat": 2, "saw": 1, "dog": 1, "and": 1} +print(counts); // ["the": 3, "cat": 2, "saw": 1, "dog": 1, "and": 1] ``` ## Making One -`map()` makes an empty map. `map(src)` makes one from an object, a struct, -another map, or an array of `[key, value]` pairs. `set()` and `set(array)` -work the same way; duplicates in the array are dropped. +`[k: v, ...]` is a map literal and `[:]` an empty map. A key is any +expression: `[name: 1]` uses the variable `name`, `["name": 1]` the string. +Writing the same literal key twice is a compile error. + +`map(src)` makes a map from a struct, its fields as keys, or from an array +of `[key, value]` pairs. `set()` makes an empty set and `set(array)` one +from an array; duplicates in the array are dropped. ```rune -print(map({ host: "localhost", port: 8080 })); // map {"host": "localhost", "port": 8080} -print(map([[200, "OK"], [404, "Not Found"]])); // map {200: "OK", 404: "Not Found"} -print(set(["a", "b", "a"])); // set {"a", "b"} +struct Server { host, port } + +let status = [200: "OK", 404: "Not Found"]; +print(status[404]); // Not Found +print(map(Server { host: "localhost", port: 8080 })); // ["host": "localhost", "port": 8080] +print(map([["a", 1], ["b", 2]])); // ["a": 1, "b": 2] +print(set(["a", "b", "a"])); // set {"a", "b"} ``` ## Keys @@ -62,17 +65,17 @@ A struct makes a natural composite key, such as a grid position: ```rune struct Pos { x, y } -let mut grid = map(); +let mut grid = [:]; grid[Pos { x: 0, y: 1 }] = "wall"; print(grid[Pos { x: 0, y: 1 }]); // wall print(grid?.[Pos { x: 9, y: 9 }] ?? "floor"); // floor ``` -Arrays, objects, maps and sets cannot be keys. To key by a pair of values, +Arrays, maps and sets cannot be keys. To key by a pair of values, use a struct as above. ```rune -let mut seen = map(); +let mut seen = [:]; seen[[1, 2]] = true; // TypeError: Cannot use an array as a key. ``` @@ -80,26 +83,26 @@ seen[[1, 2]] = true; // TypeError: Cannot use an array as a key. `m[k]` reads a key; a missing key is a `KeyError`. `m?.[k]` gives `nil` instead, so `m?.[k] ?? fallback` reads with a default. `m[k] = v` adds or -replaces a key, which needs `m` to be mutable. +replaces a key, which needs `m` to be mutable. A map is read only by +index; `m.k` is an error. ```rune -let prices = map({ apple: 1.5 }); +let prices = ["apple": 1.5]; print(prices?.["kiwi"]); // nil print(prices?.["kiwi"] ?? 0); // 0 ``` ```rune -let prices = map({ apple: 1.5 }); +let prices = ["apple": 1.5]; print(prices["kiwi"]); // KeyError: Key not found: "kiwi". ``` A value stored in a map can be changed in place through its key: ```rune -let mut groups = map(); -groups["admins"] = ["ann"]; +let mut groups = ["admins": ["ann"]]; groups["admins"].push("bob"); -print(groups); // map {"admins": ["ann", "bob"]} +print(groups); // ["admins": ["ann", "bob"]] ``` ## Map Methods @@ -111,7 +114,7 @@ print(groups); // map {"admins": ["ann", "bob"]} - `len()` — the number of keys. ```rune -let mut stock = map({ apple: 3, pear: 0, plum: 7 }); +let mut stock = ["apple": 3, "pear": 0, "plum": 7]; print("pear" in stock); // true print(stock.delete("pear")); // true @@ -154,7 +157,7 @@ alone. `for x in s` walks a set, and `for i, x in s` numbers its elements. The order is always insertion order. ```rune -let stock = map({ apple: 3, plum: 7 }); +let stock = ["apple": 3, "plum": 7]; for name, n in stock { print("{name}: {n}"); } @@ -166,11 +169,11 @@ The loop walks the map or set as it was when the loop began, so the body may change it freely: ```rune -let mut counts = map({ a: 1, b: 2 }); +let mut counts = ["a": 1, "b": 2]; for k in counts { counts[k + "!"] = 0; } -print(counts); // map {"a": 1, "b": 2, "a!": 0, "b!": 0} +print(counts); // ["a": 1, "b": 2, "a!": 0, "b!": 0] ``` ## Equality and Conversion @@ -178,16 +181,16 @@ print(counts); // map {"a": 1, "b": 2, "a!": 0, "b!": 0} `==` compares maps and sets by their entries, in any order. ```rune -print(map({ a: 1, b: 2 }) == map({ b: 2, a: 1 })); // true -print(set([1, 2]) == set([2, 1])); // true +print(["a": 1, "b": 2] == ["b": 2, "a": 1]); // true +print(set([1, 2]) == set([2, 1])); // true ``` `json.stringify` writes a set as an array, and a map with string keys as -an object, in insertion order. +a JSON object, in insertion order. ```rune -print(json.stringify(set([3, 1, 2]))); // [3,1,2] -print(json.stringify(map({ a: 1, b: 2 }))); // {"a":1,"b":2} +print(json.stringify(set([3, 1, 2]))); // [3,1,2] +print(json.stringify(["a": 1, "b": 2])); // {"a":1,"b":2} ``` Like every value, a map or a set is [copied](../language/values.md#copies) diff --git a/docs/src/data/methods.md b/docs/src/data/methods.md index 634cb137..8d7c0431 100644 --- a/docs/src/data/methods.md +++ b/docs/src/data/methods.md @@ -51,7 +51,7 @@ Money.zero().zero(); // TypeError: 'zero' is a static method of Money; call it a ## Mutating Methods A method that takes `mut self` may change the instance: set its fields -(`self.balance += 1`, `self["x"] = 1`), change what they hold +(`self.balance += 1`), change what they hold (`self.items.push(x)`) or replace `self` as a whole. Without `mut`, `self` is read-only. Mutating methods are the only place a struct's fields can be set; see [Setting Fields](structs.md#setting-fields). @@ -126,8 +126,8 @@ print(acc.balance); // 50 ### Where a Mutating Method Can Be Called The receiver must be something that can be changed: a `let mut` -variable, or an element, property or field reached from one. That -includes structs stored in arrays and objects: +variable, or an element, key or field reached from one. That includes +structs stored in arrays and maps: ```rune struct Account { owner, balance = 0 } @@ -139,6 +139,10 @@ impl Account { let mut accounts = [Account { owner: "ann" }, Account { owner: "bob" }]; accounts[1].deposit(25); print(accounts[1]); // Account {owner: "bob", balance: 25} + +let mut byOwner = ["ann": Account { owner: "ann" }]; +byOwner["ann"].deposit(10); +print(byOwner["ann"].balance); // 10 ``` On an immutable variable, a struct literal or the result of a call, there @@ -218,30 +222,24 @@ let save = Button { label: "save", onClick: () => "saved" }; save.onClick(); // TypeError: Button has no method 'onClick'; call the function in its field as (x.onClick)(). ``` -Plain objects hold data only, so a function cannot be the value of an -object property. Put behavior in a struct and its `impl`. For a lookup -table of functions by name, use a [map](map-and-set.md): +For a lookup table of functions by name, use a [map](map-and-set.md): ```rune -let greeters = map([ - ["en", name => "hello, " + name], - ["es", name => "hola, " + name], -]); +let greeters = [ + "en": name => "hello, " + name, + "es": name => "hola, " + name, +]; print(greeters["es"]("ann")); // hola, ann ``` -```rune -let handlers = { greet: name => "hi " + name }; // Error at 'greet': Property 'greet' of an object cannot hold a function. -``` - Functions of a namespace, such as `math.floor(x)` or `fs.join(a, b)`, are not methods; they are plain functions grouped under a name. ## Methods on Built-in Types `impl` also works on the built-in types `string`, `number`, `boolean`, -`array`, `object`, `function`, `map`, `set`, `bytes` and `stream`. This +`array`, `function`, `map`, `set`, `bytes` and `stream`. This lets you add the helpers your program keeps needing: ```rune diff --git a/docs/src/data/structs.md b/docs/src/data/structs.md index e079bcac..41c8c1e5 100644 --- a/docs/src/data/structs.md +++ b/docs/src/data/structs.md @@ -34,8 +34,8 @@ print(User { name, role }); // User {name: "bob", role: "admin"} A default must be a constant: a literal or operators on literals (`60 * 60`), a [`const`](../language/variables.md#constants), a variant -of an [enum](enums.md) in the same file, an array or object of these, -`map()` or `set()`. +of an [enum](enums.md) in the same file, an array or map literal of +these, or `set()`. ```rune const DAY = 24 * 60 * 60; @@ -117,7 +117,7 @@ let mut ann = User { name: "ann" }; ann.role = "admin"; // TypeError: Cannot set field 'role' of User outside its mut self methods. ``` -A struct also owns what its fields hold. An array, object, map, set or +A struct also owns what its fields hold. An array, map, set or another struct in a field is part of it, so changing it in place, `ann.tags.push(x)`, counts as setting the field: @@ -132,8 +132,8 @@ Inside a `mut self` method all of this is allowed: `self.tags.push(x)`, ## Updating with Spread -A struct literal may spread a struct of its own type, or an object, -then list the fields to change: `Config { ...base, debug: true }`. +A struct literal may spread a struct of its own type, or a map, then +list the fields to change: `Config { ...base, debug: true }`. Items apply in order, so later ones win. ```rune @@ -150,10 +150,10 @@ print(base); // Config {host: "localhost", port: 8080, debug: false} A struct of another type is a runtime error. -## From an Object +## From a Map -Spreading an object into a struct literal is how data from `json.parse` -becomes a struct: +Spreading a map with string keys into a struct literal is how data from +`json.parse` becomes a struct: ```rune struct Config { host, port = 8080 } @@ -164,52 +164,52 @@ print(cfg.port); // 8080 ``` Every key must be a field, and every field without a default must be -given, by the object or by the literal. The check runs when the literal +given, by the map or by the literal. The check runs when the literal is built: ```rune struct Config { host, port = 8080 } -Config { ...{ host: "example.com", timeout: 5 } }; // KeyError: Config has no field 'timeout'. +Config { ...["host": "example.com", "timeout": 5] }; // KeyError: Config has no field 'timeout'. ``` ```rune struct Config { host, port = 8080 } -Config { ...{ port: 80 } }; // KeyError: Missing field 'host' of Config. +Config { ...["port": 80] }; // KeyError: Missing field 'host' of Config. ``` ## Type Tests and Equality -`v is User` is `true` only for a `User`, never for an object that -happens to have the same keys; see +`v is User` is `true` only for a `User`, never for a map or another +struct with the same fields; see [Type Tests](../language/values.md#type-tests-and-patterns). `==` compares two structs of the same type field by field. A struct -never equals an object or a struct of another type. +never equals a map or a struct of another type. ```rune struct User { name, role = "member" } let ann = User { name: "ann" }; print(ann is User); // true -print({ name: "ann" } is User); // false -print(ann == User { name: "ann" }); // true -print(ann == { name: "ann", role: "member" }); // false +print(["name": "ann"] is User); // false +print(ann == User { name: "ann" }); // true +print(ann == ["name": "ann", "role": "member"]); // false ``` ## Walking the Fields -`{ ...s }` copies a struct into a plain object, its fields in -declaration order. Use it to loop over the fields or count them: +`map(s)` copies a struct into a map, its fields in declaration order. +Use it to loop over the fields or count them: ```rune struct User { name, role = "member" } let ann = User { name: "ann" }; -for field, value in { ...ann } { +for field, value in map(ann) { print("{field}: {value}"); } // name: ann // role: member -print({ ...ann }.len()); // 2 +print(map(ann).len()); // 2 ``` diff --git a/docs/src/errors-and-exit-codes.md b/docs/src/errors-and-exit-codes.md index 2aa0ebbb..a7417d14 100644 --- a/docs/src/errors-and-exit-codes.md +++ b/docs/src/errors-and-exit-codes.md @@ -62,25 +62,27 @@ it, the program stops, and the report also lists the calls the error passed through, innermost first: ```rune +struct Order { price, qty } + fun total(order) { order.price * order.qty } -let orders = [{ price: 10, qty: 2 }, 5]; +let orders = [Order { price: 10, qty: 2 }, 5]; print(orders.map(total)); ``` ``` TypeError: Cannot read property 'price' of number. - --> main.rn:2:11 + --> main.rn:4:11 | -1 | fun total(order) { -2 | order.price * order.qty +3 | fun total(order) { +4 | order.price * order.qty | ^^^^^ - at total (main.rn:2) + at total (main.rn:4) at array.map (native) - at