[swift-evolution] Add an ifPresent function to Optional

Dmitri Gribenko gribozavr at gmail.com
Mon Mar 14 17:37:23 CDT 2016


On Fri, Mar 11, 2016 at 7:17 PM, Howard Lovatt via swift-evolution
<swift-evolution at swift.org> wrote:
> @Dave, Yes, CollectionType would be better than SequenceType.

We already considered doing this a long time ago, and we decided against it.

1.  `for x in y {}` would start working everywhere because of T to T? promotion.

2.  Even if we forbid that specific promotion in that context, the following

for x in optionalThing {}

is also not good if the unwrapped value is a collection.  (The user
probably meant to iterate over the collection instead.)

3.  Why would one want all of the collection APIs on an optional?
index(of:)?  sorted()?  .first/.last?  Another overload of map that
returns an array?

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-evolution mailing list