I love the motivation here. I'm sure you'll come up with something that will help people.
There's an opportunity to ease a lot of the friction that stems from having both associated types and generic placeholders. And that's applicable to everyone, not just the inexperienced.
For example, I don't believe this proposed syntax can work…
func maxValue(in collection: some Collection<Int>) -> Int
… because, which one of Collection
's various associated types is that?
And look at how these functions have to be written so differently, based on level of genericism:
func count<Element>(array: Array<Element>) -> Int {
array.count
}
func count<Collection: Swift.Collection>(collection: Collection) -> Int {
collection.count
}
Also, while you can parameterize an associated type, or a function argument…
protocol Protocol {
associatedtype IntCollection: Collection
where IntCollection.Element == Int
}
func ƒ<IntCollection: Collection>(_: IntCollection)
where IntCollection.Element == Int { }
…you can't parameterize a typealias
.
I propose a combination of
- support of labeling of generic placeholders and associated types
- associatedtype-esque disregarding of irrelevant generic placeholders
func maxValue(in collection: Collection<Element: Int>) -> Int
func count(array: Array) -> Int {
array.count
}
func count(collection: Collection) -> Int {
collection.count
}
typealias IntCollection = Collection<Element: Int>
I don't think the some
keyword would be helpful to the programmer for this. It might be useful for decorating what would otherwise be existential parameters, for performance reasons?