silk.core.static_sequence.
Immutable lists of values that exist only during static evaluation.
When to use#
Use StaticSequence to build a list of values at compile time and then generate code for each
value with static for. Use silk.collections.list.List for a list at runtime.
Details#
A static sequence has no runtime value. It has no allocator, capacity, or address. Each operation returns a new sequence and does not change its input. All elements have the same type.
Examples#
Generate one addition for each element#
import silk.core.static_sequence { StaticSequence }
static fn weights() -> Intrinsic.StaticSequence<i32> {
let mut result = StaticSequence.empty<i32>()
result = StaticSequence.append<i32>(move result, 20)
result = StaticSequence.append<i32>(move result, 22)
return move result
}
pub fn main() -> i32 {
let mut total = 0
static for weight in weights() {
total = total + weight
}
if total != 42 {
return 1
}
return 0
}Import as StaticSequence with import silk.core.static_sequence { StaticSequence }.
Public declarations: 1.
StaticSequence#
pub struct StaticSequenceThe owner type of the static sequence operations. It has no fields and is never constructed.
Details#
Each operation is static. It runs only during static evaluation, such as in a static fn, a
static for, or a const initializer.
Implementation StaticSequence#
impl StaticSequenceAssociated function empty#
pub static fn empty<Element>() -> Intrinsic.StaticSequence<Element>Returns an empty sequence of Element values.
Method append#
pub static fn append<Element>(self: Intrinsic.StaticSequence<Element>, value: Element) -> Intrinsic.StaticSequence<
Element,
>Returns a new sequence with the elements of self and then value.
Details#
This function consumes self. To grow a sequence in a loop, assign the result back to the
same binding, as in result = StaticSequence.append<i32>(move result, 1).
Associated function concat#
pub static fn concat<Element>(
left: Intrinsic.StaticSequence<Element>,
right: Intrinsic.StaticSequence<Element>,
) -> Intrinsic.StaticSequence<Element>Returns a new sequence with the elements of left and then the elements of right.
Method length#
pub static fn length<Element>(self: Intrinsic.StaticSequence<Element>) -> usizeReturns the number of elements in self.
Method at#
pub static fn at<Element>(self: Intrinsic.StaticSequence<Element>, index: usize) -> ElementReturns the element at the zero-based position index.
Gotchas#
If index is not less than the length, static evaluation stops and the compiler reports an
error.
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.