[swift-evolution] Yet another fixed-size array spitball session

Robert Bennett rltbennett at icloud.com
Tue May 30 16:02:34 CDT 2017


I agree that Swift absolutely needs a static array type.

However, until then, here's a gist to generate the code for a static array struct: https://gist.github.com/rltbennett/8a750aa61d58746b3ca4531b3ca3d0db . Happy coding. (Disclaimer: barely tested.)


> On May 30, 2017, at 11:27 AM, David Sweeris via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>>> On May 30, 2017, at 03:25, Pavol Vaskovic <pali at pali.sk> wrote:
>>> 
>>> On Tue, May 30, 2017 at 7:51 AM, David Sweeris <davesweeris at mac.com> wrote:
>>> 
>>> `(Int, Int, Int, Int)` isn't *that* horrible compared to "[Int x 4]", but would you want to replace "[Int8 x 10000]" with the multipage-long tuple equivalent?
>> 
>> 😳
>> It would be really helpful to my understanding, if you spoke about a practical use case. This reads as a contrived counterexample to me…
>> 
>> If your type really has 10 000 values in it, why does it have to be static, why doesn't normal Array fit the bill?
> 
> Sure, I meant it as an example of how unwieldy large tuples can be. Even medium ones, really. Tuples are great for bundling a few values, but much more than that any they become annoying to work with because there's no easy way to iterate through them. As a more realistic example, what if you want a stack-allocated 256-element buffer (which is a real possibility since statically-allocated C arrays are imported as tuples)? You have to manually keep track of i, because you have to hard-code which element you're addressing ("buf.0", "buf.1", etc), rather than being able to look it up directly from an index variable like, well, an array ("buf[i]").
> 
> Plus the fact that they can't conform to protocols really limits their usefulness in filling the role of a "normal" type. For instance, even though you could easily create the normal, 64-bit hash value from an instance of a `(Int32, Int32)` simply by concatenating the two elements' bits, you can't create a `Dictionary<(Int32, Int32), SomeType>` because there's no mechanism to get `(Int, Int)` to conform to Dictionary's `Hashable` requirement.
> 
> Could both of these features get added to Tuples? From a technically PoV, sure, and it's been discussed in previous threads. IMHO we'd get more benefit out of adding support for variadic generic parameters and using literal values as generic parameters (both of which have also been previously discussed).
> 
> But it's all out of scope until after Swift 4 comes out, because none of this affects ABI or source-code compatibility.
> 
> - Dave Sweeris
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170530/ffa6c365/attachment.html>


More information about the swift-evolution mailing list