Skip to content

Instantly share code, notes, and snippets.

View hborders's full-sized avatar

Heath Borders hborders

View GitHub Profile
@hborders
hborders / MissingReturn.m
Last active May 14, 2018 20:11
An example of the compiler error when get when we miss a return that a block requires
#import "Example.h" // https://gist.github.com/hborders/2af9b39e27b62ef9e68c65085126fe4a
- (void)missingReturn {
Example * _Nonnull fooExample = [[ExampleFoo alloc] initWithF:@"foo"
g:@"goo"];
NSNumber * _Nonnull number =
[ExampleSwitcher<NSNumber *> nonnullExampleFrom:fooExample
switchFoo:^(ExampleFoo * _Nonnull foo_) {
return @(foo_.f.length + foo_.g.length);
}
@hborders
hborders / MissingAssignment.m
Created March 13, 2017 18:05
Only assigning to a __block variable in one of the blocks. I wish this was an error.
#import "Example.h" // https://gist.github.com/hborders/2af9b39e27b62ef9e68c65085126fe4a
- (void)missingAssignment {
Example * _Nonnull fooExample = [[ExampleFoo alloc] initWithF:@"foo"
g:@"goo"];
NSNumber * _Nonnull __block number;
[fooExample switchFoo:^(ExampleFoo * _Nonnull foo_) {
number = @(foo_.f.length + foo_.g.length);
}
bar:^(ExampleBar * _Nonnull bar_) {
@hborders
hborders / ExampleTestsTestTransformExample.m
Created March 13, 2017 16:51
An Objective-C pattern for making sum types - ExampleTestsTestTransformExample.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 NSNumber * _Nonnull transformExample(Example * _Nonnull example) {
return [ExampleSwitcher<NSNumber *> nonnullValueFrom:example
switchFoo:^NSNumber * _Nonnull (ExampleFoo * _Nonnull foo_) {
return @(foo_.f.length + foo_.g.length);
}
@hborders
hborders / ExampleBar.h
Last active March 13, 2017 16:44
An Objective-C pattern for making sum types - ExampleBar.h
#import "Example.h" // https://gist.github.com/hborders/2af9b39e27b62ef9e68c65085126fe4a
@interface ExampleBar : Example
@property (nonatomic, readonly) NSInteger b;
@property (nonatomic, readonly) NSInteger c;
+ (instancetype _Nonnull)new NS_UNAVAILABLE;
- (instancetype _Nonnull)init NS_UNAVAILABLE;
- (instancetype _Nonnull)initWithB:(NSInteger)b
c:(NSInteger)c NS_DESIGNATED_INITIALIZER;
@hborders
hborders / ExampleFoo.h
Last active March 13, 2017 16:44
An Objective-C pattern for making sum types - ExampleFoo.h
#import "Example.h" // https://gist.github.com/hborders/2af9b39e27b62ef9e68c65085126fe4a
@interface ExampleFoo : Example
@property (nonatomic, readonly) NSString * _Nonnull f;
@property (nonatomic, readonly) NSString * _Nonnull g;
+ (instancetype _Nonnull)new NS_UNAVAILABLE;
- (instancetype _Nonnull)init NS_UNAVAILABLE;
- (instancetype _Nonnull)initWithF:(NSString * _Nonnull)f
g:(NSString * _Nonnull)g NS_DESIGNATED_INITIALIZER;
@hborders
hborders / ExampleBar.m
Last active March 13, 2017 16:43
An Objective-C pattern for making sum types - Example.m - ExampleBar
#import "ExampleBar.h" // https://gist.github.com/hborders/f1c231a89de11157adf46ee85880733a
#import "ExamplePrivate.h" // https://gist.github.com/hborders/12fcbe34cf4de68c2317bad92a8e223d
@implementation ExampleBar
- (instancetype _Nonnull)initPrivate {
NSLog(@"Unavailable");
abort();
}
- (instancetype _Nonnull)initWithB:(NSInteger)b
c:(NSInteger)c {
@hborders
hborders / ExampleFoo.m
Last active March 13, 2017 16:28
An Objective-C pattern for making sum types - ExampleFoo
#import "ExampleFoo.h" // https://gist.github.com/hborders/094f05d932b3d7a1e389184ba525b0c3
#import "ExamplePrivate.h" // https://gist.github.com/hborders/12fcbe34cf4de68c2317bad92a8e223d
@implementation ExampleFoo
- (instancetype _Nonnull)initPrivate {
NSLog(@"Unavailable");
abort();
}
- (instancetype _Nonnull)initWithF:(NSString * _Nonnull)f
g:(NSString * _Nonnull)g {
@hborders
hborders / ExampleSwitcher.m
Last active March 13, 2017 18:45
An Objective-C pattern for making sum types - ExampleSwitcher.m
#import "ExampleSwitcher.h" // https://gist.github.com/hborders/f378b4e0d0f1d74ff7c8149cb04c6268
@implementation ExampleSwitcher
+ (NSObject * _Nonnull)nonnullValueFrom:(Example * _Nonnull)example
switchFoo:(NSObject * _Nonnull (^ _Nonnull)(ExampleFoo * _Nonnull foo_))fooBlock
bar:(NSObject * _Nonnull (^ _Nonnull)(ExampleBar * _Nonnull bar_))barBlock {
NSParameterAssert(example);
NSParameterAssert(fooBlock);
NSParameterAssert(barBlock);
@hborders
hborders / Example.m
Last active March 13, 2017 16:26
An Objective-C pattern for making sum types - Example.m
#import "ExamplePrivate.h" // https://gist.github.com/hborders/12fcbe34cf4de68c2317bad92a8e223d
@implementation Example
- (instancetype _Nonnull)initPrivate {
return [super init];
}
- (void)switchFoo:(void (^ _Nonnull)(ExampleFoo * _Nonnull foo_))fooBlock
bar:(void (^ _Nonnull)(ExampleBar * _Nonnull bar_))barBlock {
// Subclasses must implement and call
}
@hborders
hborders / ExampleSwitcher.h
Last active March 13, 2017 16:49
An Objective-C pattern for making sum types - ExampleSwitcher.h
#import "Example.h" // https://gist.github.com/hborders/2af9b39e27b62ef9e68c65085126fe4a
@interface ExampleSwitcher<ReturnType : NSObject *> : NSObject
+ (instancetype _Nonnull)new NS_UNAVAILABLE;
- (instancetype _Nonnull)init NS_UNAVAILABLE;
+ (ReturnType _Nonnull)nonnullValueFrom:(Example * _Nonnull)example
switchFoo:(ReturnType _Nonnull (^ _Nonnull)(ExampleFoo * _Nonnull foo_))fooBlock
bar:(ReturnType _Nonnull (^ _Nonnull)(ExampleBar * _Nonnull bar_))barBlock;
+ (ReturnType _Nullable)nullableValueFrom:(Example * _Nonnull)example