generics, the swift abi and you

16
SWIFT GENERICS, THE ABI AND YOU Swift Austin May 3rd, 2017

Upload: carl-brown

Post on 22-Jan-2018

151 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Generics, the Swift ABI and you

SWIFT GENERICS, THE ABI AND YOUSwift Austin

May 3rd, 2017

Page 2: Generics, the Swift ABI and you

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

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

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

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

ASSOCIATED TYPESMostly the same thing, but for ProtocolsSlide from WWDC 2016 Session 419

Page 7: Generics, the Swift ABI and you

EXAMPLE: ARRAY

funcmakeIterator()->IndexingIterator<Array<(String,String)>>

Page 8: Generics, the Swift ABI and you

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

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

ASIDE: WHAT’S ABI?Application Binary Interface: It’s what makes a language “all grown up”

Page 11: Generics, the Swift ABI and you

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

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

UPCOMING IN SWIFT 4

Page 14: Generics, the Swift ABI and you

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

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

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/