Skip to content

Instantly share code, notes, and snippets.

//------------------------------------------------------------------------------
// The following compiles and works, but see comments for 2 compiler crashers.
//------------------------------------------------------------------------------
protocol DefaultInitializable { init() }
extension Int: DefaultInitializable {}
// Hople = Homogeneous tuple / static array implemented using recursive structs.
protocol HopleType : DefaultInitializable {
typealias Element: DefaultInitializable
subscript(index: Int) -> Element { get set }
}
@jepers
jepers / Why.swift
Last active October 9, 2015 01:38
Four types that (to me) should be "the same" (although they are different types of course) needs to be initialized differently, why?
struct SA<T> {
let v: T
init(_ v: T) { self.v = v }
}
typealias SB = SA<(Int, Int)>
typealias SC = SA<W>
typealias W = (Int, Int)
@jepers
jepers / GenericSrgbGammaRemoval.swift
Created September 30, 2015 18:54
An example of creating a generic function/method working for Float and Double
// (Xcode 7.1 beta 2)
// Background:
// I wanted to replace these two overloads with a generic function/method (working for Float and Double):
// func oldRemoveSrgbGamma(v: Float) -> Float {
// return (v <= 0.04045) ? v * (1.0 / 12.92) : pow((v + 0.055) * (1.0 / (1.0 + 0.055)), 2.4)
// }
// func oldRemoveSrgbGamma(v: Double) -> Double {
// return (v <= 0.04045) ? v * (1.0 / 12.92) : pow((v + 0.055) * (1.0 / (1.0 + 0.055)), 2.4)
// }
@jepers
jepers / TypeLevelNAND.swift
Last active September 28, 2015 10:34
A compile time / type level NAND "function" in Swift.
protocol Bit {}
protocol IsSet {}
protocol IsClr {}
struct Set : Bit, IsSet {}
struct Clr : Bit, IsClr {}
protocol NandOp { typealias Result }
extension NandOp { typealias Result = Set }