Understanding Swift Generics: Type Conformance

Wayne Bishop
Sep 19, 2019 · 3 min read

The ability to create generic models in Swift provides a powerful, yet flexible model for code reuse. Generics may not come up as the main discussion in a technical interview, but if used correctly, will certainly showcase your ability to create a flexible design. Swift generics can be used to extend straightforward or complex code and is embedded in many aspects of the language. To understand how this works, let’s review the challenge of comparing Apples and Oranges:

/* 
challenge: Identify and correct the syntax and/or design issues with the following code. Assume basic type definitions for Apple and Orange are provided.
*/

func isBetter(lhs: T, rhs: T) -> T {
if lhs > rhs {
return lhs
}
return rhs
}

//hint - see parameters..
let results = isBetter(lhs: Apple, rhs: Orange)

To review, the main goal of generics is to extend a function (or object) to support different types. As shown, isBetter accepts two generic parameters and returns the larger value. We can also conclude our main objective is to compare the parameters and determine the better result. Our challenge is writing code to evaluate items that potentially differ in behavior and design.

Conforming Types

struct Orange: Comparable {    
var name: String
var taste: Int
...
}

As part of our implementation, notice how Orange now supports the popular (Swift SDK) Comparable protocol. This feature provides a set of consistent rules needed for making (logical) comparisons. For Orange to fully conform to Comparable, let’s add some additional behavior:

struct Orange: Comparable {    
var name: String
var taste: Int

static func <(lhs: Orange, rhs: Orange) -> Bool {
return lhs.taste < rhs.taste
}
}

With these changes in place let’s review our solution. Notice that isBetter also accepts generic parameters that support the Comparable type constraint:

func isBetter<T: Comparable>(lhs: T, rhs: T) -> T? {    
if lhs > rhs {
return lhs
}

else if lhs < rhs {
return rhs
}
return nil
}

...

let a = Orange(name: "Tangerine", taste: 9)
let b = Orange(name: "Clementine", taste: 5)

//works as expected
let result = isBetter(lhs: a, rhs: b)

Making Things Edible

protocol Edible: Comparable {
var taste: Int
}

With this change in place, we can now refactor the Orange type to support any type of Fruit:

func isBetter<T: Edible>(lhs: T, rhs: T) -> T? {
...
}

struct Fruit: Edible {
var taste: Int
var hasSeeds: Bool = true
...
}

let a = Fruit(name: "Apple", taste: 9)
let b = Fruit(name: "Orange", taste: 5)

let result = isBetter(lhs: a, rhs: b)

Liked this essay? Learn more about my book, course and student education programs at waynewbishop.com.

Swift Algorithms & Data Structures

Modern Code, Illustrations & Computer science

Wayne Bishop

Written by

I write about Swift Development & Computer Science. Get more tips on technical interview preparation at — www.waynewbishop.com

Swift Algorithms & Data Structures

Modern code, Illustrations & Computer science

Wayne Bishop

Written by

I write about Swift Development & Computer Science. Get more tips on technical interview preparation at — www.waynewbishop.com

Swift Algorithms & Data Structures

Modern code, Illustrations & Computer science

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store