baml.Map
An insertion-ordered collection of key-value pairs.
Signature
class baml.Map<K, V>An insertion-ordered collection of key-value pairs.
Map<K, V> is spelled map<K, V> in type position. K must be string —
the compiler rejects any other key type — so the type parameter exists for
uniformity rather than choice; convert other keys with .to_string() first.
Iteration order is the order in which keys were first inserted: keys()
and values() follow it, set on an existing key keeps that key's original
position, and delete preserves the order of the entries that remain. This
is how JavaScript's
Map
and Python's dict behave — not the arbitrary order of a hash map.
Like arrays, maps are heap objects with reference semantics: a map passed to
a function or stored in another container is shared, and every mutating
method (set, delete, clear, …) is visible through every reference.
m[key] reads and m[key] = v writes through the map. Reading a key that
is not present raises baml.panics.MapKeyNotFound; writing one inserts it
at the end. get is the non-panicking read.
When V is nullable, a null returned by get, set or delete is
ambiguous between "no such key" and "the key's value is null". has is
the way to tell the two apart.
Source:<builtin>/baml/containers.bamlbytes 28382–32563
Instance methods
clear
(self) -> nulldelete
(self, key: K) -> V | nullget
(self, key: K) -> V | nullget_or_insert
(self, key: K, default: V) -> Vhas
(self, key: K) -> boolkeys
(self) -> K[]length
(self) -> intReturns the number of entries. O(1).
set
(self, key: K, value: V) -> V | nullvalues
(self) -> V[]Returns the values, in the same order as keys() — so keys()[i] maps
to values()[i].
The array is a snapshot, but the values are not copied: a reference type is shared with the map, so mutating a value obtained here also mutates the one the map holds.
Implementations
baml.Concrete for T
Source:<builtin>/baml/core.bamlbytes 747–779
baml.ops.Equals for map<string, V>
Instance methods
eq
(self, other: map<string, V>) -> boolneq
(self, other: Self) -> boolWhether self and other differ. Defaults to !self.eq(other).
Override this only to compute the answer more directly; an override that
disagrees with !eq leaves the pair inconsistent. Equals for null
overrides it with a constant false, which is consistent because null
has exactly one value.
Source:<builtin>/baml/ns_ops/comparison.bamlbytes 13802–13952
baml.ops.Index for map<string, V>
Output = VInstance methods
index
(self, idx: string) -> VSource:<builtin>/baml/ns_ops/index.bamlbytes 2613–2765