SlideShare a Scribd company logo
SWIFT GENERICS, THE ABI AND YOU
Swift Austin
May 3rd, 2017
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.
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
ASSOCIATEDTYPES
Mostly the same thing, but for Protocols
Slide from WWDC 2016 Session 419
EXAMPLE:ARRAY
func	makeIterator()	->	IndexingIterator<Array<(String,	String)>>
EXAMPLE: UNSAFE POINTERS
reqDispatchData	=	requestBody.withUnsafeBytes	{	(ptr:	UnsafePointer<UInt8>)	->	
DispatchData	in	
												DispatchData(bytes:	UnsafeBufferPointer<UInt8>(start:	ptr,		
																									count:	requestBody.count))	
}
EXAMPLE:WEAK WRAPPER
class	Weak<T:	AnyObject>	{	
		weak	var	value	:	T?	
		init	(value:	T)	{	
				self.value	=	value	
		}	
}
extension	Array	where	Element:Weak<AnyObject>	{	
		mutating	func	reap	()	{	
				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
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)	
						//	...	
				}	
}
UPCOMING IN SWIFT 4
SE-0148 GENERIC SUBSCRIPTS
https://github.com/apple/swift-evolution/blob/master/proposals/0148-generic-
subscripts.md
extension	Dictionary	{	
		subscript<Indices:	Sequence>(indices:	Indices)	->	[Iterator.Element]	where	
Indices.Iterator.Element	==	Index	{	
				//	...	
		}	
}
SE-0142 PERMIT WHERE CLAUSES TO CONSTRAIN ASSOCIATED
TYPES
SE-0143 CONDITIONAL CONFORMANCES
https://github.com/apple/swift-evolution/blob/master/proposals/0142-associated-types-constraints.md
https://github.com/apple/swift-evolution/blob/master/proposals/0143-conditional-conformances.md
extension	Array:	Equatable	where	Element:	Equatable	{	
		static	func	==(lhs:	Array<Element>,	rhs:	Array<Element>)	->	Bool	{	...	}	
}
protocol	Sequence	{	
				associatedtype	Iterator	:	IteratorProtocol	
				associatedtype	SubSequence	:	Sequence	where	SubSequence.Iterator.Element	==	Iterator.El
				...	
}
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/

More Related Content

PDF
Dependence day insurgence
PPTX
2018-09 - F# and Fable
PDF
Raspberry using Python Session 3
PDF
Raspberry using Python Session 1
ODP
PPTX
Intro to Perfect - LA presentation
PDF
PDF
Dependence day insurgence
2018-09 - F# and Fable
Raspberry using Python Session 3
Raspberry using Python Session 1
Intro to Perfect - LA presentation

Similar to Generics, the Swift ABI and you (20)

PDF
Rootcon X - Reverse Engineering Swift Applications
PDF
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
PDF
Qtruby
PPTX
XMPP, TV and the Semantic Web
PDF
Swiftmailer
PDF
Basic Introduction to Web Development
PDF
Spring Cloud Gateway - Nate Schutta
PPTX
Tech breakfast 18
PPT
F# Intro for Scala Developers
PDF
Daniele Esposti - Evolution or stagnation programming languages - Codemotion ...
PDF
Daniele Esposti - Evolution or stagnation programming languages - Codemotion ...
PDF
Opentracing 101
PDF
Upgrading JavaScript to ES6 and using TypeScript as a shortcut
PDF
High Availability SOA APP with GlusterFS
PDF
Whoops! Where did my architecture go?
PDF
How we migrated Zalando app to Swift3?
PDF
Being Dangerous with Twig
PDF
Making Swift Even More Functional
PDF
Developer disciplines
PDF
J threads-pdf
Rootcon X - Reverse Engineering Swift Applications
Hack in the Box GSEC 2016 - Reverse Engineering Swift Applications
Qtruby
XMPP, TV and the Semantic Web
Swiftmailer
Basic Introduction to Web Development
Spring Cloud Gateway - Nate Schutta
Tech breakfast 18
F# Intro for Scala Developers
Daniele Esposti - Evolution or stagnation programming languages - Codemotion ...
Daniele Esposti - Evolution or stagnation programming languages - Codemotion ...
Opentracing 101
Upgrading JavaScript to ES6 and using TypeScript as a shortcut
High Availability SOA APP with GlusterFS
Whoops! Where did my architecture go?
How we migrated Zalando app to Swift3?
Being Dangerous with Twig
Making Swift Even More Functional
Developer disciplines
J threads-pdf
Ad

More from Carl Brown (20)

PDF
GDPR, User Data, Privacy, and Your Apps
PDF
New in iOS 11.3b4 and Xcode 9.3b4
PDF
Managing Memory in Swift (Yes, that's a thing)
PDF
Better Swift from the Foundation up #tryswiftnyc17 09-06
PDF
Swift GUI Development without Xcode
PDF
what's new in iOS10 2016-06-23
PDF
Open Source Swift: Up and Running
PDF
Parse migration CocoaCoders April 28th, 2016
PDF
Swift 2.2 Design Patterns CocoaConf Austin 2016
PDF
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
PDF
Gcd cc-150205
PDF
Cocoa coders 141113-watch
PDF
iOS8 and the new App Store
PDF
Dark Art of Software Estimation 360iDev2014
PDF
Intro to cloud kit Cocoader.org 24 July 2014
PDF
Welcome to Swift (CocoaCoder 6/12/14)
PDF
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
PPT
Introduction to Git Commands and Concepts
PDF
REST/JSON/CoreData Example Code - A Tour
KEY
360iDev iOS AntiPatterns
GDPR, User Data, Privacy, and Your Apps
New in iOS 11.3b4 and Xcode 9.3b4
Managing Memory in Swift (Yes, that's a thing)
Better Swift from the Foundation up #tryswiftnyc17 09-06
Swift GUI Development without Xcode
what's new in iOS10 2016-06-23
Open Source Swift: Up and Running
Parse migration CocoaCoders April 28th, 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Gcd cc-150205
Cocoa coders 141113-watch
iOS8 and the new App Store
Dark Art of Software Estimation 360iDev2014
Intro to cloud kit Cocoader.org 24 July 2014
Welcome to Swift (CocoaCoder 6/12/14)
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
Introduction to Git Commands and Concepts
REST/JSON/CoreData Example Code - A Tour
360iDev iOS AntiPatterns
Ad

Recently uploaded (20)

DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
Complete Guide to Website Development in Malaysia for SMEs
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PDF
CapCut Video Editor 6.8.1 Crack for PC Latest Download (Fully Activated) 2025
PPTX
assetexplorer- product-overview - presentation
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
Complete Guide to Website Development in Malaysia for SMEs
Oracle Fusion HCM Cloud Demo for Beginners
Computer Software and OS of computer science of grade 11.pptx
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Design an Analysis of Algorithms II-SECS-1021-03
CHAPTER 2 - PM Management and IT Context
Adobe Illustrator 28.6 Crack My Vision of Vector Design
iTop VPN Free 5.6.0.5262 Crack latest version 2025
CapCut Video Editor 6.8.1 Crack for PC Latest Download (Fully Activated) 2025
assetexplorer- product-overview - presentation
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Advanced SystemCare Ultimate Crack + Portable (2025)
Salesforce Agentforce AI Implementation.pdf
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
Wondershare Filmora 15 Crack With Activation Key [2025
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free

Generics, the Swift ABI and you