Skip to content

Instantly share code, notes, and snippets.

View glessard's full-sized avatar

Guillaume Lessard glessard

  • California, USA
View GitHub Profile
@dabrahams
dabrahams / FactoryInitialization.swift
Last active October 22, 2022 13:46
Class factory initializers
/// Classes whose initializers actually create derived classes
protocol FactoryInitializable {
/// The type of the least-derived class declared to be FactoryInitializable.
///
/// - Warning: Do not define this in your FactoryInitializable type!
associatedtype FactoryBase: AnyObject, FactoryInitializable = Self
// This associatedtype is a trick that captures `Self` at the point where
// `FactoryInitializable` enters a class hierarchy; in other contexts, `Self`
// refers to the most-derived type.
}
@dabrahams
dabrahams / Dispatch.swift
Created April 25, 2020 13:37
Post-hoc specialized behavior based on refinement
/// A repository of functionality depending on the conformances of `Model`.
///
/// Conditional conformances provide implementation functions that take a
/// generic argument type with the safe assumption that the argument's concrete
/// type is `Model`.
struct Dispatch<Model> {
/// Returns `f(a as! Model) as! R1`
///
/// Used by implementation functions to avoid the clutter of casting
/// explicitly.