Skip to content

Instantly share code, notes, and snippets.

@hborders
Last active March 13, 2017 16:42
Show Gist options
  • Save hborders/2af9b39e27b62ef9e68c65085126fe4a to your computer and use it in GitHub Desktop.
Save hborders/2af9b39e27b62ef9e68c65085126fe4a to your computer and use it in GitHub Desktop.
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;
// Consumers will use this method to unpack the sum type
- (void)switchFoo:(void (^ _Nonnull)(ExampleFoo * _Nonnull foo_))fooBlock
bar:(void (^ _Nonnull)(ExampleBar * _Nonnull bar_))barBlock NS_REQUIRES_SUPER;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment