Sign in

    any-collection

    Typed references over mutable and immutable maps of dynamically typed values

    any
    collection
    context
    map
    moonbit
    Download zip
    Author
    Version
    0.2.2
    License
    MIT
    Last updated
    2 months ago
    Downloads
    148

    #any-collection

    totto2727/any-collection is a MoonBit module for mutable and persistent immutable maps whose values are stored as Yoorkin/any.Any and read through reusable typed references.

    This document is canonical README.mbt.md; maintain README.md as the relative symlink README.md -> README.mbt.md.

    #Usage

    ///|
    test {
    let request_id : @any_collection.AnyRef[String, String] =
    @any_collection.AnyRef::AnyRef("request_id")
    let values = @any_collection.AnyMutableMap::AnyMutableMap([
    request_id.entry("request-1"),
    ])
    debug_inspect(values.get(request_id), content="Some(\"request-1\")")
    }

    #Key features

    • Reusable typed references keep each key's value type explicit.
    • Mutable and persistent immutable map wrappers use the same reference API.
    • Fallback getters make missing keys and conversion errors easy to handle.

    #Prerequisites

    • MoonBit: Install the MoonBit toolchain and package manager.

    #Setup

    1. Add the module to a MoonBit project.

    moon add totto2727/any-collection@0.2.2

    #API

    #Development

    For repository structure and development commands, see AGENTS.md.

    #License

    This README was generated from the share-artifact skill and README template.

    #any-collection

    totto2727/any-collection provides mutable and persistent immutable maps whose values are stored as Yoorkin/any.Any and retrieved through reusable typed references.

    This document is canonical src/README.mbt.md; the module overview remains at the repository README.

    #Usage

    ///|
    test {
    let request_id : AnyRef[String, String] = AnyRef::AnyRef("request_id")
    let retry_count : AnyRef[String, Int] = AnyRef::AnyRef("retry_count")

    let mutable = AnyMutableMap::AnyMutableMap(
    [request_id.entry("request-1"), retry_count.entry(2)],
    capacity=8,
    )
    debug_inspect(mutable.get(request_id), content="Some(\"request-1\")")
    mutable.set(retry_count, 3)
    inspect(mutable.get_or(retry_count, 0), content="3")
    inspect(mutable.map.length(), content="2")

    let immutable = AnyImmutableHashMap::AnyImmutableHashMap([
    request_id.entry("request-2"),
    ])
    let updated = immutable.added(request_id, "request-3")
    debug_inspect(immutable.get(request_id), content="Some(\"request-2\")")
    debug_inspect(updated.get(request_id), content="Some(\"request-3\")")
    inspect(updated.map.contains(request_id.key), content="true")
    }

    #Key features

    • AnyRef[K, T] keeps a key and its typed encoder/decoder together.
    • AnyMutableMap mutates in place, while AnyImmutableHashMap returns a new map from added.
    • Both wrappers expose their underlying map for operations that do not need typed conversion.

    #Prerequisites

    • MoonBit: Install the MoonBit toolchain and package manager.
    • Yoorkin/any: The module dependency supplies Any and Anyable.

    #Setup

    1. Add the module to a MoonBit project.

    moon add totto2727/any-collection@0.2.2

    #API

    #Typed access

    AnyMutableMap::AnyMutableMap(entries, capacity?) delegates to the underlying Map constructor, while AnyImmutableHashMap::AnyImmutableHashMap(entries) delegates to @immut.HashMap. Pass [] to create an empty collection, use AnyRef::entry for typed constructor entries, or provide raw Yoorkin/any.Any pairs.

    get returns None for an absent key and raises the original conversion error when the stored runtime type does not match the reference. get_or replaces absence and conversion errors with its default; get_or_none replaces conversion errors with None while preserving successful and missing results.

    Define one shared AnyRef[K, T] for each key and value type. Core MoonBit types already implement Yoorkin/any.Anyable; custom values must extend Yoorkin/any.Payload and implement Anyable, as shown in examples/basic/main.mbt.

    #Development

    For repository structure and development commands, see AGENTS.md.

    #License

    This README was generated from the share-artifact skill and README template.

    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.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io