liveBook · Manning

Protocol extension

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

A Swift extension mechanism that adds implementations and other members to a protocol or its conforming types.

A protocol extension supplies fully implemented functions and members to a protocol, letting conforming graph types reuse core behavior before a concrete graph type exists. section 4.1

A graph protocol extension can provide:

  • Vertex lookup by index or by an Equatable vertex value. section 4.1

Reusable graph behavior

An UnweightedGraph<String> stores cities as vertices and uses inherited extension methods to add edges such as Seattle–Chicago and Seattle–San Francisco, then prints adjacency lists.

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Implements counts, lookup, neighbors, and edge storage, but does not develop protocol extensions beyond the graph framework.
Examples
Uses UnweightedGraph<String>, city vertices, bidirectional edges, neighbor lookup, and printed adjacency lists.
What it assumes
Assumes protocols, conformances, classes, generic types, Equatable, and adjacency-list graph representation.
What you can do after
Build it from scratch by defining a graph protocol and supplying shared operations through an extension.
How it connects
Connects protocols with associated types, Equatable vertices, CustomStringConvertible, generic graph types, and adjacency lists.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.