Skip to content

Instantly share code, notes, and snippets.

@hborders
Last active March 13, 2017 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hborders/06af193c6f44788fa3bb5d84c8818cc0 to your computer and use it in GitHub Desktop.
Save hborders/06af193c6f44788fa3bb5d84c8818cc0 to your computer and use it in GitHub Desktop.
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 {
NSParameterAssert(f);
NSParameterAssert(g);
self = [super initPrivate];
if (self) {
_f = f;
_g = g;
}
return self;
}
- (void)switchFoo:(void (^ _Nonnull)(ExampleFoo * _Nonnull foo_))fooBlock
bar:(void (^ _Nonnull)(ExampleBar * _Nonnull bar_))barBlock {
NSParameterAssert(fooBlock);
NSParameterAssert(barBlock);
[super switchFoo:fooBlock
bar:barBlock];
fooBlock(self);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment