Skip to content

Instantly share code, notes, and snippets.

@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 }
@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 / 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)
//------------------------------------------------------------------------------
// 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 }
}
struct True {}
struct False {}
protocol ResultPossessingType {}
extension ResultPossessingType { typealias Result = False }
struct IsIntegerType<T> : ResultPossessingType {
static func printResult() { print("Static method prints \(self).Result:", Result.self, self.Result.self) } // <-- Added self.Result.self
}
extension IsIntegerType where T: IntegerType {
typealias Result = True
@jepers
jepers / Hmm.swift
Created November 18, 2015 19:26
An example of where I get confused by the interplay between associated types, typealiases and protocol extensions.
// I'm using the concept of bits just to make the code a bit more concrete.
// The actual use case has nothing to do with implementing type level bits ...
// This code compiles and runs as is, but note the last line.
// Q: Should this program compile without errors? If so, what should the last line print?
protocol BitType {}
struct Clr : BitType {}
struct Set : BitType {}
protocol BitAndOp {
typealias A: BitType = Clr
@jepers
jepers / test.swift
Last active January 26, 2016 03:50
Example of something that takes a long time to type check.
// This little program takes ~ 6 seconds to compile on my MacBook Pro Late 2013.
// It compiles and works as expected. But the type checker spends a lot of time
// on the function at the end.
// In my actual project, I have much more code like this, and it results in very
// long compile times. So I'd like to know if there is something I can do to
// speed that up.
protocol DefaultInitializable { init() }
extension Float : DefaultInitializable {}
//----------------------------------------------------------------------------------------------------
// This program compiles and runs. Question: Can generic struct X be made to satisfy TEST?
//----------------------------------------------------------------------------------------------------
protocol DefaultInitializable { init() }
extension Double : DefaultInitializable {}
extension Float : DefaultInitializable {}
extension Int : DefaultInitializable {}
extension UInt8 : DefaultInitializable {}
// ...
protocol CountType { static var value: Int { get } }
@jepers
jepers / test.swift
Last active November 29, 2015 03:07
Testing Swift's ability to optimize code using simple custom Float4 and Float4x4 value types, comparing to SIMD counterparts, subscript implementation turns out to make a big difference for F4x4 while not for F4 ...
//==============================================================================
// This program is possibly demonstrating an issue/opportunity for improvement
// of the optimizer.
// (Tested with Xcode 7.2 beta 4, OS X 10.10.5, Macbook Pro late 2013)
// (Compiled (-O -gnone) as command line app)
//==============================================================================
//
// There is a test at the end which measures the time it takes to do some
// matrix (and vector) type operations.
//
protocol DefaultInitializable { init() }
protocol StaticStorageType : DefaultInitializable {
typealias Element: DefaultInitializable = Self
static var staticCount: Int { get }
}
extension StaticStorageType {
typealias Plus1 = StaticStorageOfOneMoreThan<Self>
typealias Plus2 = Plus1.Plus1
typealias Plus3 = Plus2.Plus1
typealias Times2 = StaticStorageDoubled<Self>