Skip to content

Instantly share code, notes, and snippets.

View hborders's full-sized avatar

Heath Borders hborders

View GitHub Profile
@hborders
hborders / Example.h
Last active March 13, 2017 16:42
An Objective-C pattern for making sum types - Example.h
#import <Foundation/Foundation.h>
@class ExampleFoo;
@class ExampleBar;
@interface Example : NSObject
// notice there is no public designated initializer, so consumers can't instantiate Example directly.
// They must instantiate a subclass.
+ (instancetype _Nonnull)new NS_UNAVAILABLE;
- (instancetype _Nonnull)init NS_UNAVAILABLE;
@hborders
hborders / ExampleTestsTestPrintExample.m
Last active March 13, 2017 16:51
An Objective-C pattern for making sum types - ExampleTestsTestPrintExample.m
#import "Example.h" // https://gist.github.com/hborders/e00d7ff965ca8f8df8f7e762a2269efb
#import "ExampleSwitcher.h" // https://gist.github.com/hborders/f378b4e0d0f1d74ff7c8149cb04c6268
#import "ExampleFoo.h" // https://gist.github.com/hborders/094f05d932b3d7a1e389184ba525b0c3
#import "ExampleBar.h" // https://gist.github.com/hborders/f1c231a89de11157adf46ee85880733a
static void printExample(Example * _Nonnull example) {
[example switchFoo:^(ExampleFoo * _Nonnull foo_) {
NSLog(@"Foo: %@, %@", foo_.f, foo_.g);
}
bar:^(ExampleBar * _Nonnull bar_) {
@hborders
hborders / ExamplePrivate.h
Last active March 13, 2017 16:26
An Objective-C pattern for making sum types - ExamplePrivate.h
#import "Example.h" // https://gist.github.com/hborders/e00d7ff965ca8f8df8f7e762a2269efb
#import "ExampleFoo.h" // https://gist.github.com/hborders/094f05d932b3d7a1e389184ba525b0c3
#import "ExampleBar.h" // https://gist.github.com/hborders/f1c231a89de11157adf46ee85880733a
@interface Example ()
- (instancetype _Nonnull)initPrivate NS_DESIGNATED_INITIALIZER;
@end
@interface ExampleFoo()
- (instancetype _Nonnull)initPrivate NS_UNAVAILABLE;
@hborders
hborders / BadExampleEnum.swift
Created March 13, 2017 15:35
The swift compiler saves us from mismatching our enum types
enum Example {
case Foo(f: String, g: String)
case Bar(b: Int, c: Int)
}
func printExample(_ example: Example) {
switch example {
case .Foo(let f, let g):
print("Foo: \(f), \(g)")
case .Bar(let b, let c):
@hborders
hborders / BadExample.c
Last active March 13, 2017 15:26
An example of accidental union misuse of a C union with an enum tag to differentiate types
#include <stdio.h>
typedef enum {
FooType,
BarType,
} Type;
typedef struct {
char *f;
char *g;
@hborders
hborders / Example.c
Created March 13, 2017 15:14
A C union with an enum tag to differentiate types
#include <stdio.h>
typedef enum {
FooType,
BarType,
} Type;
typedef struct {
char *f;
char *g;
@hborders
hborders / ExampleEnum.swift
Last active March 13, 2017 15:29
A simple Swift enum example
enum Example {
case Foo(f: String, g: String)
case Bar(b: Int, c: Int)
}
func printExample(_ example: Example) {
switch example {
case .Foo(let f, let g):
print("Foo: \(f), \(g)")
case .Bar(let b, let c):
@hborders
hborders / TWFoundation.tree
Last active February 10, 2017 16:22
TWFoundation tree
TWFoundation.podspec # the podspec that our Twitch iOS App and the TWFoundationHost project below consume
TWFoundation
├── Podfile # Configures TWFoundationHost and TWFoundationHostTests
├── Podfile.lock
├── Pods
│   ├── ... # Pods stuff
├── TWFoundation
│   ├── NSArray+TWFoundation.h
│   ├── NSArray+TWFoundation.m
| ├── ... # More TWFoundation classes
@hborders
hborders / NSCFMutableSetWithOpaquePointersTests.m
Last active September 24, 2016 00:41
Shows that CFMutableSetRef doesn't transfer CFSetCallBacks to NSSet returned from setByAddingObject:
#define HJBAssertNotSame(expression1, expression2, ...) \
XCTAssert((expression1) != (expression2), \
__VA_ARGS__)
- (void)testCFMutableSetWithOpaquePointersCopiedToNSSetByAddingDoesNotKeepOpaquePointerBehavior
{
NSString * _Nonnull string1 = [@"ab" stringByAppendingString:@"cd"];
NSString * _Nonnull string2 = [@"a" stringByAppendingString:@"bcd"];
TWAssertNotSame(string1,
@hborders
hborders / git_hash_from_cfbundleversion.bash
Created September 14, 2016 18:19
Convert a `CFBundleVersion` string constructed with `versions.bash` into a short revision of a git commit hash
#!/bin/bash -euo pipefail
if [ ${#} -eq 0 ]
then
# read from STDIN
MAYBE_CFBUNDLEVERSION=$( cat )
else
MAYBE_CFBUNDLEVERSION="${1}"
fi