Skip to content

Instantly share code, notes, and snippets.

@hborders
Last active March 13, 2017 16:51
Show Gist options
  • Save hborders/02b12ba1059946409bdaa555c1441520 to your computer and use it in GitHub Desktop.
Save hborders/02b12ba1059946409bdaa555c1441520 to your computer and use it in GitHub Desktop.
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_) {
NSLog(@"Bar: %@, %@", @(bar_.b), @(bar_.c));
}];
}
- (void)testPrintExample {
Example * _Nonnull fooExample = [[ExampleFoo alloc] initWithF:@"foo"
g:@"goo"];
Example * _Nonnull barExample = [[ExampleBar alloc] initWithB:2
c:3];
printExample(fooExample); // Foo: foo, goo
printExample(barExample); // Bar: 2, 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment