December 2018
Intermediate to advanced
414 pages
10h 19m
English
Thanks to Swift and its expressive generics, it is possible to abstract the visitor pattern through a series of protocols:
protocol Visitor { func visit<T>(element: T) where T:Visitable}
A Visitor is an object that can traverse Visitable elements. It implements a single method, which is generic:
protocol Visitable { func accept(visitor: Visitor)}
Visitable objects can be traversed by a Visitor. Here we don't use the generics, as they're not needed:
// Default implementation for our visitable nodesextension Visitable { func accept(visitor: Visitor) { visitor.visit(element: self) }}
For the sake of simplicity, and because this is the general use case, we'll want the Visitable objects simply to call visit on the ...
Read now
Unlock full access