Skip to content

Instantly share code, notes, and snippets.


Andy Dent AndyDentFree

View GitHub Profile
AndyDentFree / demoDefer.cpp
Last active Aug 29, 2015
Shows how modern C++ can use RAII helper objects to provide the "defer" feature just introduced in Swift 2.0
View demoDefer.cpp
// shows how C++11 can implement the "defer" introduced in Swift 2.0
#include <iostream>
using simpleClosure = std::function<void()>; // new typedef syntax used for closure
// class only needs to be declared once then used all over to wrap closures
// see
class deferRAII {
deferRAII(simpleClosure cl) : runOnDestruction(cl) {}
AndyDentFree / typeVariantsDemo.swift
Created May 20, 2015
typeVariantsDemo in Swift
View typeVariantsDemo.swift
// the Swift direct equivalent of the F# gist
import Darwin
enum Shape {
case Circle (radius:Double)
case Rectangle (width:Double, height:Double)
case Triangle (tbase:Double, height:Double)
func area(shape:Shape) -> Double {
AndyDentFree / typeVariantsDemo
Created May 20, 2015
Working example (from Xamarin Studio) demonstrating Type Variants and discriminated unions
View typeVariantsDemo
open Microsoft.FSharp.Math
type Shape =
| Circle of radius:float
| Rectangle of width:float * height:float // tuple of float, float
| Triangle of tbase:float * height:float
let area shape =
match shape with
| Circle radius -> Math.PI * radius * radius
| Rectangle (width, height) -> width * height
AndyDentFree / safeParentViewController.h
Last active Aug 29, 2015
Using Objective-C++ for type-safe walking up parent views
View safeParentViewController.h
Sometimes in iOS code, you need to be able to walk up the chain of parent views, especially if working on legacy code.
This has been hardcoded in the past with horrible assumptions like this, which broke again in iOS 8:
if (IS_IOS7_AND_UP) {
spdVC = (SuperDuperViewController*)[(UITableView*)self.superview.superview.superview.superview.superview delegate];
} else {
spdVC = (SuperDuperViewController*)[(UITableView*)self.superview.superview.superview delegate];