silk.core.static_map.
Maps from text keys to values that exist only during static evaluation.
When to use#
Use StaticMap to look values up by name while a static fn runs, such as the options a
command line parser derives from its declared fields. Use silk.collections.hash_map.HashMap
for a map at runtime.
Details#
A static map has no runtime value. It keeps its keys in insertion order, and each operation returns a new map without changing its input. A lookup compares keys one by one, which suits the few dozen entries a static description holds.
Examples#
Count the options of each kind#
import silk.core.option { Option }
import silk.core.static_map { StaticMap }
static fn shorts() -> StaticMap<u8> {
let mut result = StaticMap<u8>.empty()
result = StaticMap<u8>.insert(move result, "verbose", 118)
result = StaticMap<u8>.insert(move result, "quiet", 113)
return move result
}
static fn shortOf(name: string<'static>) -> u8 {
let map = shorts()
return match StaticMap<u8>.get(&map, name) {
Option<u8>.Some {value} => value
Option<u8>.None => 0
}
}
pub fn main() -> i32 {
static if shortOf("quiet") != 113 { compileError("quiet has no short form") }
return 0
}Import as StaticMap with import silk.core.static_map { StaticMap }.
Public declarations: 1.
StaticMap#
pub struct StaticMap<Value>An immutable map from string<'static> keys to Value values, built during static evaluation.
Details#
Every operation is static. Inserting a key that is already present replaces its value and
keeps its position.
Implementation StaticMap<Value>#
impl<Value> StaticMap<Value>Associated function empty#
pub static fn empty() -> SelfReturns a map with no entries.
Method length#
pub static fn length(self: &Self) -> usizeReturns the number of entries in self.
Method position#
pub static fn position(self: &Self, key: string<'static>) -> Option<usize>Returns the position of key in insertion order, if self has it.
Method contains#
pub static fn contains(self: &Self, key: string<'static>) -> boolWhether self has an entry for key.
Method get#
pub static fn get(self: &Self, key: string<'static>) -> Option<Value>Returns the value for key, if self has it.
Method insert#
pub static fn insert(self: Self, key: string<'static>, value: Value) -> SelfReturns a map with the entries of self and key mapped to value.
Details#
A key already present keeps its position and takes the new value. To build a map in a loop,
assign the result back to the same binding, as in map = StaticMap<u8>.insert(move map, "a", 1).
Method keys#
pub static fn keys(self: &Self) -> Intrinsic.StaticSequence<string<'static>>Returns the keys of self in insertion order.
Method values#
pub static fn values(self: &Self) -> Intrinsic.StaticSequence<Value>Returns the values of self in the order of their keys.
Category: Core.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.