[swift-evolution] [Pitch?] Way to declare a Swift Array that guarantees it can't be empty

Charles Constant charles at charlesism.com
Tue Aug 8 03:22:33 CDT 2017


This isn't a fully formed pitch, and maybe already discussed, but...

If we have have optionals and non-optionals, shouldn't we also have a way
to declare that an Array never be empty? It seems like this would naturally
lead to more elegant designs.

Here's a use-case:

struct Wavelet {
var buff: [Double]
var sign: Sign
var peak: Double {
return buff.find_max()! // <-- Yuck!
}
}

In my app, I never want to create an empty "buff" here. If I could declare
that the Array always contain at least 1 element, I wouldn't need to worry
about a whole bunch of unwrapping elsewhere in my program. Native ability
to do this would also be handy to store chunks of memory, too?

Would this be worthwhile?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170808/48698f7b/attachment.html>


More information about the swift-evolution mailing list