try-get on a Vec out of range is None; OrderedMap takes set and update

This commit is contained in:
Linus Björnstam 2026-10-04 14:13:51 +02:00
parent 48771a225a
commit ea96af593c

View file

@ -594,13 +594,13 @@ Data is often a record holding a map holding a vec of records. Reading something
| @code{(update value step ... f)} | a copy of @code{value} with @code{(f old)} at the end of the path |
}
A key is read with @code{ref} and written with @code{ref-set}, so the path can go through anything that implements the traits behind them: @code{Refable} to read, and @code{Settable} to write, which @code{Vec} and @code{Map} do. A field step can read a .NET property too, as in @code{(get team .players 0 .name .-Length)}, but only a Bjolang record or struct can be copied with a field changed, so @code{set} and @code{update} refuse one.
A key is read with @code{ref} and written with @code{ref-set}, so the path can go through anything that implements the traits behind them: @code{Refable} to read, and @code{Settable} to write, which @code{Vec}, @code{Map} and @code{OrderedMap} do. A field step can read a .NET property too, as in @code{(get team .players 0 .name .-Length)}, but only a Bjolang record or struct can be copied with a field changed, so @code{set} and @code{update} refuse one.
Some details worth knowing:
@itemlist{
@item{@code{set} does not read the last key, so setting a key a map does not have adds it. A key that is missing earlier in the path throws, as @code{ref} does.}
@item{@code{try-get} gives @code{None} for a missing map key, but a vec index out of range is still an error, as it is for @code{try-ref}. The keys after a missing one are not evaluated.}
@item{@code{try-get} gives @code{None} for a missing map key or a vec index out of range. The keys after a missing one are not evaluated.}
@item{@code{set} and @code{update} evaluate the value, then each key from left to right, then the new value or the function, each of them exactly once.}
@item{These are macros, so a step is a field or a key by how it is written, not by its type: @code{.name} is always a field, and anything else is always a key.}
}
@ -1248,7 +1248,7 @@ Many operations are traits, so they work on every collection that implements the
| @code{(length c)} @code{(empty? c)} | lists, vecs, arrays, maps, strings | size, and whether it is empty |
| @code{(ref c key)} | vecs, arrays, maps, strings | the element at an index or key; throws |
| @code{(try-ref c key)} | the same | the element as an @code{Option} |
| @code{(ref-set c key x)} | vecs, maps | a copy with @code{x} at the key |
| @code{(ref-set c key x)} | vecs, maps, ordered maps | a copy with @code{x} at the key |
| @code{(add c x)} @code{(add-all c xs)} | vecs, maps, sets | a new collection with more in it |
| @code{(add! b x)} @code{(add-all! b xs)} | builders and transients | put things in, in place |
| @code{(fold f init c)} | anything you can loop over | a left fold |