2. THIS IS A *BIG* TOPIC
And I’m not a compiler expert, and Swift’s
implementation is in flux.
(Plus it’s been a long week already)
If Reality disagrees, it wins.
3. THIS IS NOT
SUPPOSED TO BE A
LECTURE
*Please* interrupt if you have questions,
opinions, disagreements,etc.
This will be much better as a discussion.
4. QUICK INTRODUCTION
• Generic functions can work with any type.
• They help reduce boilerplate and duplication
• (From https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/
Generics.html)
func swapTwoValues<T>(_ a: inout T, _ b: inout T) {
let temporaryA = a
a = b
b = temporaryA
}
5. QUICK HISTORY (IN SWIFT)
Slide from WWDC 2016 Session 402
Generics were new (more or less) in Swift 3
11. WHAT DOES THAT HAVE TO DO WITH
GENERICS?
• Chris Lattner:“…including some of the most important generics
features needed in order to lock down the ABI of the standard
library.As such, the generics and ABI stability goals will roll into a
future release of Swift, where I expect them to be the highest priority
features to get done.”
• From http://ericasadun.com/2016/05/16/winding-down-swift-3-0-
abi-stability-deferred/
12. SO, WHAT ARE WE STILL MISSING?
• Variadic Generics
• Generic value parameters
• Extensions of structural types
• Currently, only nominal types (classes, structs, enums, protocols) can be extended. One could imagine extending
structural types--particularly tuple types--to allow them to, e.g., conform to protocols
public func zip<... Sequences : SequenceType>(... sequences: Sequences...)
-> ZipSequence<Sequences...> {
return ZipSequence(sequences...)
} struct MultiArray<T, let Dimensions: Int> {
// specify the number of dimensions to the array
subscript (indices: Int...) -> T {
get {
require(indices.count == Dimensions)
// ...
}
}