Skip to content

Instantly share code, notes, and snippets.

View twof's full-sized avatar
🐦
Having an animated discussion with the Swift compiler

Alex Reilly twof

🐦
Having an animated discussion with the Swift compiler
View GitHub Profile
func add(a: Int, b: Int) -> Int{
return a + b
}
func add(a: Int, b: Int) -> Int{
return a + b
}
func add(a: Double, b: Double) -> Double{
return a + b
}
func add<T: Numeric>(a: T, b: T) -> T{
return a + b
}
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
let temporaryA = a
a = b
b = temporaryA
}
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
let temporaryA = a
a = b
b = temporaryA
}
func swapTwoDoubles(_ a: inout Double, _ b: inout Double) {
let temporaryA = a
a = b
b = temporaryA
func swapTwoValues<T>(_ a: inout T, _ b: inout T) {
let temporaryA = a
a = b
b = temporaryA
}
class Node{
var content: Int
var next: Node
init(content: Int, next: Node){
self.content = content
self.next = next
}
}
class Node<T>{
var content: T
var next: Node?
init(content: T, next: Node?){
self.content = content
self.next = next
}
}
class Node<T>{
var content: T
var next: Node?
init(content: T, next: Node?){
self.content = content
self.next = next
}
}
@twof
twof / ReflectiveDecodable.swift
Created March 3, 2018 05:46
Codable that automatically decodes with default vals
import Foundation
func iterateEnum<T: Hashable>(_: T.Type) -> AnyIterator<T> {
var i = 0
return AnyIterator {
let next = withUnsafeBytes(of: &i) { $0.load(as: T.self) }
if next.hashValue != i { return nil }
i += 1
return next
}