[swift-evolution] [Idea] Generic subscripts
brent at architechies.com
Wed Aug 3 02:25:10 CDT 2016
I'm looking for consensus on, and a coalition for, including generic subscripts in Phase 1 of the Swift 4 design cycle.
While prototyping my deferred [SE-0132], I ran into trouble introducing `RangeExpression`, an abstraction to make the introduction of subrange features easier. Since RangeExpression is a protocol compatible with any index type, it has to have an associated type, and thus cannot be handled with an existential. I therefore had to add an overloaded subscript for each type, which partially defeated the purpose of having the protocol.
The lack of generic subscripts also forced a nasty workaround for a convenience subscript in [SE-0131]. That proposal extends `Dictionary where Key == AnyHashable` (actually a hidden `_AnyHashableProtocol`, but that's a different story) with a convenience subscript which takes any `Hashable` type—except that, because generic subscripts are impossible, [it instead has to take a hidden `_Hashable` type instead][anyhash-subscript].
The generics manifesto suggests a third use case: [a subscript that can take any Collection of Index][manifesto].
The lack of this feature at the very least impacts binary compatibility directly. It also affects source compatibility in that features like subranges are designed around its absence, forcing workarounds which affect userspace. I see good reasons to do it now and few to delay.
As mentioned, SE-0131 and SE-0132 would have benefited from this feature.
After a brief and mostly positive [discussion], Harlan Haskins and Robert Widmann submitted a [pull request] late in the Swift 3 cycle for generic and throwing subscripts. Personally, I think throwing subscripts are something we should include, but they're a separate issue and may not be appropriate for Phase 1; I would suggest severing the two parts of the proposal.
What I'd like to figure out at this point is:
* Who is interested in this feature?
* Should it be severed from throwing subscripts?
* Does the core team agree that this is in Phase 1's scope?
* Do the people who might be able to implement this have any comments on it?
[pull request]: <https://github.com/apple/swift-evolution/pull/372>
More information about the swift-evolution