> On Jan 19, 2017, at 4:17 PM, Xiaodi Wu via swift-evolution
> <[email protected]> wrote:
>
> For those times when you _don't_ know how many elements there are, don't
> care, and for some reason can't be bothered to get `array.count`, but you
> need to explicitly access an element by its index *and* have a useful
> fallback value, IMO it's reasonable to have an alternative subscript like the
> proposed `array[lenient: 10]`. But with facilities like `for...in`, `map`,
> etc., and others like `count` and `enumerated`, it's hard to argue that it's
> nearly as common a scenario as those where you are given a known-good index.
>
I’m not sure why people keep asking for this; the extension is trivial so
anyone who wants it can have it:
extension Collection
{
subscript(ifExists index: Index) -> Iterator.Element? {
guard index < self.endIndex else { return nil }
return self[index]
}
}
// won't assert!
myArray[ifExists: 42]
Russ
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution