Contents

init(capacity:initializingWith:)

Creates an array with the specified capacity, and then calls the given closure with an output span covering the array’s uninitialized memory.

Declaration

init<E>(capacity: Int, initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void) throws(E) where E : Error

Parameters

  • capacity:

    The number of elements to allocate space for in the new array.

  • initializer:

    A closure that initializes the elements of the new array.

    • Parameters:

      • span: An OutputSpan covering uninitialized memory with space for the specified number of elements.

Discussion

Inside the closure, initialize elements by appending to the OutputSpan. The OutputSpan keeps track of memory’s initialization state, ensuring safety. Its count at the end of the closure will become the count of the newly-initialized array.