Skip to content

Instantly share code, notes, and snippets.

@bartzy
bartzy / Fill.swift
Created January 8, 2020 18:01
Codable Fills
import UIKit
struct Color: Codable {
let r: Double
let g: Double
let b: Double
}
struct Point: Codable {
let x: Double
/*
This gives the error: 'SomeGenericStruct' requires the types 'Self' and 'Self.SomeProtocolItemType.SomeProtocolElement' be equivalent
*/
protocol SomeProtocolItem {
associatedtype SomeProtocolElement
func apply(_ element: SomeProtocolElement)
}
@bartzy
bartzy / TypeErasure.swift
Created September 9, 2016 13:30 — forked from russbishop/TypeErasure.swift
Type erasure with multiple adopting types
// Paste me into a playground!
import Cocoa
//: # Basic Setup
protocol FancyProtocol {
associatedtype Thing
func holdPinkyUp(x: Thing)
}
protocol Binder: Sequence {
associatedtype Paper
/* How do I force adoping types of Binder to be a Sequence that iterates over their Paper associated type? */
}
struct Book: Binder {
typealias Paper = ThickPaper
}
/* Binders should adopt Sequence */
protocol BinderProtocol : Sequence {
associatedtyped Paper
var width : Int { get }
var height : Int { get }
}
// Paper types
struct ThickPaper {
@bartzy
bartzy / pats.swift
Last active September 6, 2016 17:05
PATs, PATs everywhere!
/** Instead of this (PAT): **/
protocol Coin {
associatedtyped Material
}
// Materials
struct Silver { ... }
struct Gold { ... }
// Adoping types