LiteralSlice

public struct LiteralSlice[T] { /* private fields */ }

Read-only view over the compiler-emitted backing buffer for an array literal.

User code rarely names this type directly: it appears in ExpressibleByArrayLiteral.init(arrayLiteral:) and friends so that types accepting [a, b, c] literals can iterate the elements without touching raw pointers. The slice does not own the storage — the compiler keeps the literal alive for the duration of the call.

Examples

// Conforming to ExpressibleByArrayLiteral public struct MyVec[T]: ExpressibleByArrayLiteral { type Element = T public init(arrayLiteral lit: LiteralSlice[T]) { var v = MyVec(); for x in lit { v.push(x) } self = v } }

Memory Model

Non-owning. The backing storage is compiler-managed and lives for the scope of the literal expression. Capturing a LiteralSlice past that scope is a use-after-free.

Properties

public var count: Int64 { get }

Number of elements in the literal.

public var isEmpty: Bool { get }

true for [].

Initializers

public init(pointer: lang.ptr[T], count: lang.i64)

Builds the slice from the raw pointer and count the compiler emits.

Subscripts

public subscript(checked: Int64) -> T? { get }

Reads element index, returning .None on out-of-bounds.

public subscript(Int64) -> T { get }

Reads element index, panicking on out-of-bounds.

The default subscript: trades a single comparison for a guaranteed trap on bad input. Use (unchecked:) inside compiler-emitted init paths where the index is statically known in range, or (checked:) to handle out-of-range without a panic.

Errors

Panics with "LiteralSlice index out of bounds" if index < 0 or index >= count.

public subscript(unchecked: Int64) -> T { get }

Reads element index without bounds checking.

Safety

Undefined behavior if index < 0 or index >= count. Compiler- emitted init paths that use this guarantee the index is in range; do not expose this subscript to user input without checking count first.

ImplementsIterable

Associated Types

type Item = T
type TargetIterator = LiteralSliceIterator[T]

Methods

public func iter() -> LiteralSliceIterator[T]

Iterator over the elements in source order.

Defined in lang/std/memory/literal_slice.ks