[swift-evolution] [RFC] Definitive Initialization and Incompatibilities with Fixed-size Arrays

Chris Lattner clattner at nondot.org
Sat Jul 22 14:02:27 CDT 2017


On Jul 18, 2017, at 1:00 PM, Daryle Walker via swift-evolution <swift-evolution at swift.org> wrote:
>> On Jul 17, 2017, at 3:26 AM, Félix Cloutier <felixcca at yahoo.ca> wrote:
>> 
>> I think that you're getting ahead of yourself. Fixed-size arrays are still useful even if they have to have been demonstrably initialized before you can use dynamic indices with them. A ton of people have already gotten into the habit of writing `int foo[40] = {}` in C.
> 
> The person with the initial suggestion regrets this habit and deliberately doesn’t want it. In other words, don’t support the waste of cycles to pre-initialize just for the elements to be immediately paved over with the real initial data. And we may make arrays of types that are heavy to initialize and may not have a default initializer, so even using a default-value term at declaration will lead to a big waste of cycles.
> 
> We eventually have to decide how to modify deterministic initialization. Do we take the safe path with dynamic deterministic initialization (and add extra resources)? Or the fast path with suspension of checks (and risk undefined behavior)? We’re probably going to lean toward the former, especially since we can limit its scope (and therefore cost).

In my opinion, there is an easy three step plan :-) to solving this problem, riffing on Array:

1) Fixed size arrays should have an initializer to init all the elements to some concrete value.
2) They should have an init that takes a closure, and runs it once per element, passing in the index.
3) Either through a magic value or a third initializer, it should be possible to *explicitly* create a fixed size array with uninitialized garbage for the elements.  This is important for specific optimizations, and should also come to Array as well.

IMO, it isn’t a problem that C allows arrays to be uninitialized - the problem is that it is a really bad default.

-Chris




More information about the swift-evolution mailing list