baml.Map

An insertion-ordered collection of key-value pairs.

Reference version

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 2838232563

Instance methods

function

clear

(self) -> null
function

delete

(self, key: K) -> V | null
function

get

(self, key: K) -> V | null
function

get_or_insert

(self, key: K, default: V) -> V
function

has

(self, key: K) -> bool
function

keys

(self) -> K[]
function

length

(self) -> int

Returns the number of entries. O(1).

function

set

(self, key: K, value: V) -> V | null
function

values

(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 747779

baml.ops.Equals for map<string, V>

Instance methods

function

eq

(self, other: map<string, V>) -> bool
function

neq

(self, other: Self) -> bool

Whether 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 1380213952

baml.ops.Index for map<string, V>

Output = V

Instance methods

function

index

(self, idx: string) -> V

Source:<builtin>/baml/ns_ops/index.bamlbytes 26132765