Swift Proposal: InlineArray
This proposal introduces a new type to the standard library,
InlineArray
, which is a fixed-size array. This is analogous to the classical C arraysT[N]
, C++’sstd::array<T, N>
, and Rust’s arrays[T; N]
.[…]
It’s important to understand that
Array
is a heap allocated growable data structure which can be expensive and unnecessary in some situations. The next best thing is to force a known quantity of elements onto the stack, probably by using tuples.[…]
We introduce a new top level type,
InlineArray
, to the standard library which is a fixed-size contiguously inline allocated array. We’re defining “inline” as using the most natural allocation pattern depending on the context of where this is used. It will be stack allocated most of the time, but as a class property member it will be inline allocated on the heap with the rest of the properties.InlineArray
will never introduce an implicit heap allocation just for its storage alone.[…]
InlineArray
will be a simple noncopyable struct capable of storing other potentially noncopyable elements. It will be conditionally copyable only when its elements are.
Initialization is special-cased to avoid constructing a temporary array from the literal, and it does not conform to Sequence
or Collection
.
We do plan to propose new protocols that look like
Sequence
andCollection
that avoid implicit copying making them suitable for types likeInlineArray
and containers of noncopyable elements. SE-0437 Noncopyable Standard Library Primitives goes into more depth about this rationale and mentions that creating new protocols to support noncopyable containers with potentially noncopyable elements are all marked as future work.[…]
With the introduction of
InlineArray
, we have a unique opportunity to fix another pain point within the language with regards to C interop. Currently, the Swift compiler imports a C array of typeT[24]
as a tuple ofT
with 24 elements.
But they haven’t decided how to do that yet without breaking compatibility.
Previously:
3 Comments RSS · Twitter · Mastodon
Swift keeps getting more ridiculous. A high-level, modern language designed to shield us from memory management—now introducing a feature to decide if an array goes on the heap or stack. What’s next? Three horns all playing La Cucaracha? I was never a fan of Objective-C or the C-based languages, but boy, are they looking good now. Swift truly is the Homer Car of programming languages.
An `InlineArray` story without a C interop story... what's the point? And it doesn't conform to Sequence and/or Collection means people will just run `map()` on it to turn it into an iterable collection.
I will say two positive things about Swift: it's always good for a morning laugh, and it makes me feel smart enough to design my own programming language.