generics, the swift abi and you
TRANSCRIPT
SWIFT GENERICS, THE ABI AND YOUSwift Austin
May 3rd, 2017
THIS IS A *BIG* TOPICAnd 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.
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.
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 }
QUICK HISTORY (IN SWIFT)Slide from WWDC 2016 Session 402
Generics were new (more or less) in Swift 3
ASSOCIATED TYPESMostly the same thing, but for ProtocolsSlide from WWDC 2016 Session 419
EXAMPLE: ARRAY
funcmakeIterator()->IndexingIterator<Array<(String,String)>>
EXAMPLE: UNSAFE POINTERS
reqDispatchData=requestBody.withUnsafeBytes{(ptr:UnsafePointer<UInt8>)->DispatchDatain
DispatchData(bytes:UnsafeBufferPointer<UInt8>(start:ptr,count:requestBody.count))}
EXAMPLE: WEAK WRAPPERclassWeak<T:AnyObject>{weakvarvalue:T?init(value:T){self.value=value}}
extensionArraywhereElement:Weak<AnyObject>{mutatingfuncreap(){self=self.filter{nil!=$0.value}}}
From: http://stackoverflow.com/a/24128121
ASIDE: WHAT’S ABI?Application Binary Interface: It’s what makes a language “all grown up”
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/
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
publicfunczip<...Sequences:SequenceType>(...sequences:Sequences...)->ZipSequence<Sequences...>{returnZipSequence(sequences...)} structMultiArray<T,letDimensions:Int>{
//specifythenumberofdimensionstothearraysubscript(indices:Int...)->T{get{require(indices.count==Dimensions)//...}}
UPCOMING IN SWIFT 4
SE-0148 GENERIC SUBSCRIPTShttps://github.com/apple/swift-evolution/blob/master/proposals/0148-generic-
subscripts.mdextensionDictionary{subscript<Indices:Sequence>(indices:Indices)->[Iterator.Element]whereIndices.Iterator.Element==Index{//...}}
SE-0142 PERMIT WHERE CLAUSES TO CONSTRAIN ASSOCIATED TYPES
SE-0143 CONDITIONAL CONFORMANCEShttps://github.com/apple/swift-evolution/blob/master/proposals/0142-associated-types-constraints.mdhttps://github.com/apple/swift-evolution/blob/master/proposals/0143-conditional-conformances.md
extensionArray:EquatablewhereElement:Equatable{staticfunc==(lhs:Array<Element>,rhs:Array<Element>)->Bool{...}}
protocolSequence{associatedtypeIterator:IteratorProtocolassociatedtypeSubSequence:SequencewhereSubSequence.Iterator.Element==Iterator.Element...}
RESOURCES:• https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md
• https://github.com/apple/swift/blob/master/docs/Generics.rst
• https://github.com/apple/swift/blob/master/docs/ABIStabilityManifesto.md
• https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Generics.html
• https://www.natashatherobot.com/swift-protocols-with-associated-types/