[swift-evolution] [Idea] NonEmptyArray

David Sweeris davesweeris at mac.com
Tue Jan 17 03:57:56 CST 2017


> On Jan 17, 2017, at 03:40, Charlie Monroe via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I've come across multiple cases, where you simply know the array is never empty and hence the optionality on first, last and behavior of a few other members is slightly different. Also, there are cases where you want to declare that you shouldn't pass an empty array e.g. into an initializer.
> 
> I was wondering whether Swift could have a specialized NonEmptyArray that could be used throughout the stdlib - e.g. String.components(separatedBy:) would return NonEmptyArray.
> 
> Thoughts?

I've tried to make such a type a few times... The struct itself isn't hard ("var first:T; var tail:[T]"), but I never could figure out how to make `NonEmptyArray` conform to `ExpressibleByArrayLiteral` (because the protocol doesn't allow for failable inits) without just crashing if there wasn't at least one element.

Anyway, I'm not opposed to adding it, as long as there's a non-crashy way to assign array literals to them.

- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170117/d742137d/attachment.html>


More information about the swift-evolution mailing list