#totto2727/x/any_collection

    Store values of different types in a mutable or persistent map and read them through typed key references. See the module README for setup and package imports.

    #Usage

    Update a retry count in a mutable map and create a persistent snapshot without changing its baseline.

    ///|
    test "update a retry context without changing its baseline snapshot" {
    let request_id : @any_collection.AnyRef[String, String] = @any_collection.AnyRef::AnyRef(
    "request_id",
    )
    let retry_count : @any_collection.AnyRef[String, Int32] = @any_collection.AnyRef::AnyRef(
    "retry_count",
    )
    let live_context = @any_collection.AnyMutableMap::AnyMutableMap([])
    live_context.set(request_id, "req-42")
    live_context.set(retry_count, 0)
    live_context.set(retry_count, live_context.get_or(retry_count, 0) + 1)

    let baseline = @any_collection.AnyImmutableHashMap::AnyImmutableHashMap([
    request_id.entry("req-42"),
    retry_count.entry(0),
    ])
    let retry_snapshot = baseline.added(
    retry_count,
    live_context.get_or(retry_count, 0),
    )

    inspect(live_context.get_or(request_id, ""), content="req-42")
    inspect(live_context.get_or(retry_count, 0), content="1")
    inspect(baseline.get_or(retry_count, 0), content="0")
    inspect(retry_snapshot.get_or(retry_count, 0), content="1")
    }

    #Migrate from totto2727/any-collection

    Replace the standalone module requirement with "totto2727/x@0.9.0" and change the package import to "totto2727/x/any_collection" @any_collection. Keep the alias to reuse existing call sites. Update both sides of public APIs that exchange map or reference types, because the old and new package types are distinct. Keep a direct Yoorkin/any dependency if your code imports it, for example to implement @any.Anyable for a custom value type.

    #API

    AnyImmutableHashMap

    pub struct AnyImmutableHashMap[K] {
    map :
    HashMap
    [K,
    Any
    ]
    }

    An immutable hash map of dynamically typed values accessed through AnyRef.

    The public map field exposes the underlying persistent hash map for operations that do not need typed conversion, including membership, removal, length, emptiness, iteration, merging, and direct Any values.

    AnyImmutableHashMap::AnyImmutableHashMap

    fn[K : Hash + Eq] AnyImmutableHashMap::AnyImmutableHashMap(entries : ArrayView[(K,
    Any
    )]) -> AnyImmutableHashMap[K]

    Creates an immutable any hash map from key-value pairs.

    The entries have the same meaning as the underlying @immut.HashMap constructor. Pass [] for an empty map. Entries may be produced with AnyRef::entry or supplied as raw @any.Any pairs.

    AnyImmutableHashMap::added

    fn[K : Hash + Eq, T] AnyImmutableHashMap::added(self : AnyImmutableHashMap[K], reference : AnyRef[K, T], value : T) -> AnyImmutableHashMap[K]

    Returns a map associating value with reference without changing this map. The same AnyRef instance can be used with mutable and immutable maps.

    AnyImmutableHashMap::get

    fn[K : Hash + Eq, T] AnyImmutableHashMap::get(self : AnyImmutableHashMap[K], reference : AnyRef[K, T]) -> T? raise

    Returns the value selected by reference when it exists with type T.

    A missing key or a value stored through a differently typed reference returns None or raises the conversion error, respectively.

    AnyImmutableHashMap::get_or

    fn[K : Hash + Eq, T] AnyImmutableHashMap::get_or(self : AnyImmutableHashMap[K], reference : AnyRef[K, T], default : T) -> T

    Returns the selected value, replacing a missing key or conversion error with default.

    AnyImmutableHashMap::get_or_none

    fn[K : Hash + Eq, T] AnyImmutableHashMap::get_or_none(self : AnyImmutableHashMap[K], reference : AnyRef[K, T]) -> T?

    Returns the selected value, replacing a conversion error with None.

    AnyMutableMap

    pub struct AnyMutableMap[K] {
    map : Map[K,
    Any
    ]
    }

    A mutable map of dynamically typed values accessed through AnyRef.

    The public map field exposes the underlying Map[K, @any.Any] for operations that do not need typed conversion, including membership, removal, length, emptiness, iteration, merging, and direct Any writes.

    AnyMutableMap::AnyMutableMap

    fn[K : Hash + Eq] AnyMutableMap::AnyMutableMap(entries : ArrayView[(K,
    Any
    )], capacity? : Int) -> AnyMutableMap[K]

    Creates a mutable any map from key-value pairs.

    The entries and optional capacity have the same meaning as the underlying Map constructor. Pass [] for an empty map. Entries may be produced with AnyRef::entry or supplied as raw @any.Any pairs.

    AnyMutableMap::get

    fn[K : Hash + Eq, T] AnyMutableMap::get(self : AnyMutableMap[K], reference : AnyRef[K, T]) -> T? raise

    Returns the value selected by reference when it exists with type T.

    A missing key or a value stored through a differently typed reference returns None or raises the conversion error, respectively.

    AnyMutableMap::get_or

    fn[K : Hash + Eq, T] AnyMutableMap::get_or(self : AnyMutableMap[K], reference : AnyRef[K, T], default : T) -> T

    Returns the selected value, replacing a missing key or conversion error with default.

    AnyMutableMap::get_or_none

    fn[K : Hash + Eq, T] AnyMutableMap::get_or_none(self : AnyMutableMap[K], reference : AnyRef[K, T]) -> T?

    Returns the selected value, replacing a conversion error with None.

    AnyMutableMap::set

    fn[K : Hash + Eq, T] AnyMutableMap::set(self : AnyMutableMap[K], reference : AnyRef[K, T], value : T) -> Unit

    Associates value with reference, replacing an existing value.

    AnyRef

    pub struct AnyRef[K, T] {
    key : K
    // private fields
    }

    A reusable typed reference to one keyed value in an any collection.

    The same reference can be used with both AnyMutableMap and AnyImmutableHashMap.

    AnyRef::AnyRef

    Creates a typed reference for key.

    Callers should define one shared reference per key and value type. Creating references with the same key but different value types makes mismatched reads raise the conversion error unless a fallback getter is used. T must implement Yoorkin/any.Anyable; MoonBit core types already have implementations. Custom values must extend Yoorkin/any.Payload and implement Anyable.

    AnyRef::entry

    fn[K, T] AnyRef::entry(self : AnyRef[K, T], value : T) -> (K,
    Any
    )

    Creates a key-value entry for collection initialization without exposing the Yoorkin/any encoding step to callers.