Generics, the Swift ABI and you
-
Upload
carl-brown -
Category
Software
-
view
152 -
download
2
Transcript of Generics, the Swift ABI and you
![Page 1: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/1.jpg)
SWIFT GENERICS, THE ABI AND YOUSwift Austin
May 3rd, 2017
![Page 2: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/2.jpg)
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.
![Page 3: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/3.jpg)
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.
![Page 4: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/4.jpg)
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 }
![Page 5: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/5.jpg)
QUICK HISTORY (IN SWIFT)Slide from WWDC 2016 Session 402
Generics were new (more or less) in Swift 3
![Page 6: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/6.jpg)
ASSOCIATED TYPESMostly the same thing, but for ProtocolsSlide from WWDC 2016 Session 419
![Page 7: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/7.jpg)
EXAMPLE: ARRAY
funcmakeIterator()->IndexingIterator<Array<(String,String)>>
![Page 8: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/8.jpg)
EXAMPLE: UNSAFE POINTERS
reqDispatchData=requestBody.withUnsafeBytes{(ptr:UnsafePointer<UInt8>)->DispatchDatain
DispatchData(bytes:UnsafeBufferPointer<UInt8>(start:ptr,count:requestBody.count))}
![Page 9: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/9.jpg)
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
![Page 10: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/10.jpg)
ASIDE: WHAT’S ABI?Application Binary Interface: It’s what makes a language “all grown up”
![Page 11: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/11.jpg)
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/
![Page 12: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/12.jpg)
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)//...}}
![Page 13: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/13.jpg)
UPCOMING IN SWIFT 4
![Page 14: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/14.jpg)
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{//...}}
![Page 15: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/15.jpg)
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...}
![Page 16: Generics, the Swift ABI and you](https://reader035.fdocuments.net/reader035/viewer/2022062523/5a65050c7f8b9a0d7f8b48cf/html5/thumbnails/16.jpg)
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/