any-collection

Typed references over mutable and immutable maps of dynamically typed values

any
collection
context
map
moonbit
moon add totto2727/any-collection@0.2.1
Download zip
Author
Version
0.2.1
License
MIT
Last updated
6 days ago
Downloads
59

Dependencies

README

#any-collection

totto2727/any-collection provides mutable and immutable maps whose values are stored as Yoorkin/any.Any and retrieved through reusable typed references. Keys are supplied when references are defined and may use any type supported by the underlying map.

#Usage

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

let mutable = @any_collection.AnyMutableMap::AnyMutableMap([
request_id.entry("request-1"),
retry_count.entry(2),
])
debug_inspect(mutable.get(request_id), content="Some(\"request-1\")")
debug_inspect(mutable.get(retry_count), content="Some(2)")
let retry_text : @any_collection.AnyRef[String, String] =
@any_collection.AnyRef::AnyRef("retry_count")
inspect(mutable.get_or(retry_text, "fallback"), content="fallback")
debug_inspect(mutable.get_or_none(retry_text), content="None")
inspect(mutable.map.length(), content="2")

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

The constructors mirror the underlying collection interfaces: AnyMutableMap(entries, capacity?) delegates to Map(entries, capacity?), while AnyImmutableHashMap(entries) delegates to @immut.HashMap(entries). Pass [] to create an empty collection. Use AnyRef::entry to create a (K, Yoorkin/any.Any) constructor entry without handling Any directly. Raw Any entries remain accepted.

AnyMutableMap::set mutates its map. AnyImmutableHashMap::added returns a new map and preserves its source map. Both map types accept the same AnyRef[K, T] instance.

The wrappers only provide the operations that require a typed reference and value conversion. Use the public map field directly for operations such as contains, remove, length, is_empty, iteration, and merging. Direct insertion of Any values is allowed.

get returns None when a key is absent and raises the original Yoorkin/any conversion error when the stored runtime type does not match the reference's type. get_or replaces both absence and conversion errors with its default value. get_or_none preserves successful and missing results while replacing conversion errors with None.

Define one shared AnyRef[K, T] for each key and value type so mismatched references cannot be introduced accidentally.

Value types must implement Yoorkin/any.Anyable. The dependency provides implementations for MoonBit core types. Custom types must extend Yoorkin/any.Payload and implement Anyable; see src/examples/basic/main.mbt for a complete example.

#
AnyImmutableHashMap

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

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

#
AnyImmutableHashMap::AnyImmutableHashMap

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

Creates an immutable any hash map from key-value 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.

#
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.

#
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.

#
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.

#
AnyRef::entry

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

Creates a key-value entry for collection initialization.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io