Share
Embed
Brian Gesiak | Swift API Design: Getting Results
Presented at www.swiftsummit.com
Swift Summit
March 21, 2015
Transcript
136 slides
-
@interface Banana : NSObject /** Whether the banana is peeled.
*/ @property (readonly) BOOL peeled; /** Whether the banana is delicious. @warning Do not use before the banana is peeled. */ @property (readonly) BOOL delicious; @end -
@interface Banana : NSObject /** Whether the banana is peeled.
*/ @property (readonly) BOOL peeled; /** Whether the banana is delicious. @warning Do not use before the banana is peeled. */ @property (readonly) BOOL delicious; @end -
@interface Banana : NSObject /** Whether the banana is peeled.
*/ @property (readonly) BOOL peeled; /** Whether the banana is delicious. @warning Do not use before the banana is peeled. */ @property (readonly) BOOL delicious; @end -
@interface Banana : NSObject /** Whether the banana is peeled.
*/ @property (readonly) BOOL peeled; /** Whether the banana is delicious. @warning Do not use before the banana is peeled. */ @property (readonly) BOOL delicious; @end -
BKBanana *banana = [[BKBanana alloc] init]; if (banana.delicious) { //
... } Caught "NSInternalInconsistencyException" º -
enum Banana { /** An unpeeled banana has no taste.
*/ case Unpeeled /** A peeled banana has a boolean to indicate whether it's delicious. */ case Peeled(delicious: Bool) } -
enum Banana { /** An unpeeled banana has no taste.
*/ case Unpeeled /** A peeled banana has a boolean to indicate whether it's delicious. */ case Peeled(delicious: Bool) } -
enum Banana { /** An unpeeled banana has no taste.
*/ case Unpeeled /** A peeled banana has a boolean to indicate whether it's delicious. */ case Peeled(delicious: Bool) } -
let banana = Banana.Unpeeled switch banana { case .Unpeeled: //
... case .Peeled(let delicious): if (delicious) { // ... } } -
let banana = Banana.Unpeeled switch banana { case .Unpeeled: //
... case .Peeled(let delicious): if (delicious) { // ... } } -
let banana = Banana.Unpeeled switch banana { case .Unpeeled: //
... case .Peeled(let delicious): if (delicious) { // ... } } -
Banana [[ if } let banana = Banana.Unpeeled switch banana
{ case .Unpeeled: // ... case .Peeled(let delicious): if (delicious) { // ... } } -
Banana *banana = [[Banana alloc] init]; if (banana.delicious) { //
... } let switch case case } } -
@interface MonkeyDB : NSObject /** Creates a database backed by
the store at the given URL. */ - (instancetype)initWithURL:(NSURL *)url; /** Loads a monkey with the given name, or nil if one doesn't exist. */ - (Monkey *)monkeyWithName:(NSString *)name error:(NSError **)error; @end -
@interface MonkeyDB : NSObject /** Creates a database backed by
the store at the given URL. */ - (instancetype)initWithURL:(NSURL *)url; /** Loads a monkey with the given name, or nil if one doesn't exist. */ - (Monkey *)monkeyWithName:(NSString *)name error:(NSError **)error; @end -
@interface MonkeyDB : NSObject /** Creates a database backed by
the store at the given URL. */ - (instancetype)initWithURL:(NSURL *)url; /** Loads a monkey with the given name, or nil if one doesn't exist. */ - (Monkey *)monkeyWithName:(NSString *)name error:(NSError **)error; @end -
@interface MonkeyDB (Tricycles) /** Looks up whether the given monkey
owns a tricycle. If the monkey owns one, returns the tricycle. If not, returns nil. If an error occurs, the error pointer will be populated. */ - (Tricycle *)tricycleForMonkey:(Monkey *)monkey error:(NSError **)error; @end -
@interface MonkeyDB (Tricycles) /** Looks up whether the given monkey
owns a tricycle. If the monkey owns one, returns the tricycle. If not, returns nil. If an error occurs, the error pointer will be populated. */ - (Tricycle *)tricycleForMonkey:(Monkey *)monkey error:(NSError **)error; @end -
NSURL *url = [NSURL fileURLWithPath:@".monkeydb"]; MonkeyDB *database = [[MonkeyDB alloc]
initWithURL:url]; NSError *monkeyError = nil; Monkey *monkey = [database monkeyWithName:@"Peepers" error:&monkeyError]; if (monkey == nil || monkeyError != nil) { // ...error handling. return nil; } NSError *tricycleError = nil; Tricycle *tricycle = [database tricycleForMonkey:monkey error:&tricycleError]; -
NSURL *url = [NSURL fileURLWithPath:@".monkeydb"]; MonkeyDB *database = [[MonkeyDB alloc]
initWithURL:url]; NSError *monkeyError = nil; Monkey *monkey = [database monkeyWithName:@"Peepers" error:&monkeyError]; if (monkey == nil || monkeyError != nil) { // ...error handling. return nil; } NSError *tricycleError = nil; Tricycle *tricycle = [database tricycleForMonkey:monkey error:&tricycleError]; -
NSURL *url = [NSURL fileURLWithPath:@".monkeydb"]; MonkeyDB *database = [[MonkeyDB alloc]
initWithURL:url]; NSError *monkeyError = nil; Monkey *monkey = [database monkeyWithName:@"Peepers" error:&monkeyError]; if (monkey == nil || monkeyError != nil) { // ...error handling. return nil; } NSError *tricycleError = nil; Tricycle *tricycle = [database tricycleForMonkey:monkey error:&tricycleError]; -
NSURL *url = [NSURL fileURLWithPath:@".monkeydb"]; MonkeyDB *database = [[MonkeyDB alloc]
initWithURL:url]; NSError *monkeyError = nil; Monkey *monkey = [database monkeyWithName:@"Peepers" error:&monkeyError]; if (monkey == nil || monkeyError != nil) { // ...error handling. return nil; } NSError *tricycleError = nil; Tricycle *tricycle = [database tricycleForMonkey:monkey error:&tricycleError]; -
// ...error handling. return nil; } NSError *tricycleError = nil;
Tricycle *tricycle = [database tricycleForMonkey:monkey error:&tricycleError]; if (tricycle == nil || tricycleError != nil) { // ...error handling. return nil; } return tricycle.brandName; -
// ...error handling. return nil; } NSError *tricycleError = nil;
Tricycle *tricycle = [database tricycleForMonkey:monkey error:&tricycleError]; if (tricycle == nil || tricycleError != nil) { // ...error handling. return nil; } return tricycle.brandName; -
// ...error handling. return nil; } NSError *tricycleError = nil;
Tricycle *tricycle = [database tricycleForMonkey:monkey error:&tricycleError]; if (tricycle == nil || tricycleError != nil) { // ...error handling. return nil; } return tricycle.brandName; -
NSURL *url = [NSURL fileURLWithPath:@".monkeydb"]; MonkeyDB *database = [[MonkeyDB alloc]
initWithURL:url]; Monkey *monkey = [database monkeyWithName:@"Peepers" error:nil]; Tricycle *tricycle = [database tricycleForMonkey:monkey error:nil]; return tricycle.brandName; -
NSURL *url = [NSURL fileURLWithPath:@".monkeydb"]; MonkeyDB *database = [[MonkeyDB alloc]
initWithURL:url]; Monkey *monkey = [database monkeyWithName:@"Peepers" error:nil]; Tricycle *tricycle = [database tricycleForMonkey:monkey error:nil]; return tricycle.brandName; Caught "NSInternalInconsisntencyException" º -
NSURL *url = [NSURL fileURLWithPath:@".monkeydb"]; MonkeyDB *database = [[MonkeyDB alloc]
initWithURL:url]; Monkey *monkey = [database monkeyWithName:@"Peepers" error:nil]; Tricycle *tricycle = [database tricycleForMonkey:monkey error:nil]; return tricycle.brandName; Thread 1: EXC_BAD_INSTRUCTION -
/** The result of an operation that could have failed.
*/ enum Result<SuccessType, ErrorType> { /** If the operation succeeded, the result has a value. */ case Success(SuccessType) /** If the operation failed, the result holds an error. */ case Failure(ErrorType) } -
/** The result of an operation that could have failed.
*/ enum Result<SuccessType, ErrorType> { /** If the operation succeeded, the result has a value. */ case Success(SuccessType) /** If the operation failed, the result holds an error. */ case Failure(ErrorType) } -
/** The result of an operation that could have failed.
*/ enum Result<SuccessType, ErrorType> { /** If the operation succeeded, the result has a value. */ case Success(SuccessType) /** If the operation failed, the result holds an error. */ case Failure(ErrorType) } -
struct MonkeyDB { init(url: NSURL) func monkey(name: String) -> Result<Monkey,
NSError> func tricycle(monkey: Monkey) -> Result<Tricycle, NSError> } -
struct MonkeyDB { init(url: NSURL) func monkey(name: String) -> Result<Monkey,
NSError> func tricycle(monkey: Monkey) -> Result<Tricycle, NSError> } -
struct MonkeyDB { init(url: NSURL) func monkey(name: String) -> Result<Monkey,
NSError> func tricycle(monkey: Monkey) -> Result<Tricycle, NSError> } -
let result = database.tricycle(monkey) return result.brandName 'Result<Tricycle, NSError>' does not
have a member named 'brandName' -
let monkeyResult = database.monkey(“Peepers") switch monkeyResult { case .Failure(let error):
return .Failure(error) case .Success(let monkey): let tricycleResult = database.tricycle(monkey) switch tricycleResult { case .Failure(let error): return .Failure(error) case .Success(let tricycle): return .Success(tricycle.brandName) } } -
let monkeyResult = database.monkey(“Peepers") switch monkeyResult { case .Failure(let error):
return .Failure(error) case .Success(let monkey): let tricycleResult = database.tricycle(monkey) switch tricycleResult { case .Failure(let error): return .Failure(error) case .Success(let tricycle): return .Success(tricycle.brandName) } } -
let monkeyResult = database.monkey(“Peepers") switch monkeyResult { case .Failure(let error):
return .Failure(error) case .Success(let monkey): let tricycleResult = database.tricycle(monkey) switch tricycleResult { case .Failure(let error): return .Failure(error) case .Success(let tricycle): return .Success(tricycle.brandName) } } -
let monkeyResult = database.monkey(“Peepers") switch monkeyResult { case .Failure(let error):
return .Failure(error) case .Success(let monkey): let tricycleResult = database.tricycle(monkey) switch tricycleResult { case .Failure(let error): return .Failure(error) case .Success(let tricycle): return .Success(tricycle.brandName) } } -
let monkeyResult = database.monkey(“Peepers") switch monkeyResult { case .Failure(let error):
return .Failure(error) case .Success(let monkey): let tricycleResult = database.tricycle(monkey) switch tricycleResult { case .Failure(let error): return .Failure(error) case .Success(let tricycle): return .Success(tricycle.brandName) } } -
let monkeyResult = database.monkey(“Peepers") switch monkeyResult { case .Failure(let error):
return .Failure(error) case .Success(let monkey): let tricycleResult = database.tricycle(monkey) switch tricycleResult { case .Failure(let error): return .Failure(error) case .Success(let tricycle): return .Success(tricycle.brandName) } } -
enum Result<SuccessType, ErrorType> { func map<NewType>(transform: SuccessType -> NewType) ->
Result<NewType, ErrorType> func flatMap<NewType>( transform: SuccessType -> Result<NewType, ErrorType>) -> Result<NewType, ErrorType> } -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult let tricycleResult = monkeyResult.flatMap { return database.tricycle($0) } -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { (monkey:
Monkey) in return database.tricycle(monkey) } let brandNameResult = tricycleResult.map { (tricycle: Tricycle) in return tricycle.brandName } return brandNameResult let tricycleResult = monkeyResult.flatMap { return database.tricycle($0) } let brandNameResult = tricycleResult.map { return $0.brandName } -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { return
database.tricycle($0) } let brandNameResult = tricycleResult.map { return $0.brandName } return brandNameResult -
let monkeyResult = database.monkey("Peepers") let tricycleResult = monkeyResult.flatMap { return
database.tricycle($0) } let brandNameResult = tricycleResult.map { return $0.brandName } return brandNameResult let brandNameResult = tricycleResult.map { $0.brandName } let tricycleResult = monkeyResult.flatMap { database.tricycle($0) } -
let url = NSURL( fileURLWithPath: "/Users/modocache/Gift")! let latestCommitMessage = openRepository(url)
.flatMap { $0.headReference } .flatMap { $0.commit } .flatMap { $0.message } switch latestCommitMessage { case .Success(let message): println(message) case .Failure(let error): println(error.localizedDescription) } -
let url = NSURL( fileURLWithPath: "/Users/modocache/Gift")! let latestCommitMessage = openRepository(url)
.flatMap { $0.headReference } .flatMap { $0.commit } .flatMap { $0.message } switch latestCommitMessage { case .Success(let message): println(message) case .Failure(let error): println(error.localizedDescription) } -
let url = NSURL( fileURLWithPath: "/Users/modocache/Gift")! let latestCommitMessage = openRepository(url)
.flatMap { $0.headReference } .flatMap { $0.commit } .flatMap { $0.message } switch latestCommitMessage { case .Success(let message): println(message) case .Failure(let error): println(error.localizedDescription) } -
let url = NSURL( fileURLWithPath: "/Users/modocache/Gift")! let latestCommitMessage = openRepository(url)
.flatMap { $0.headReference } .flatMap { $0.commit } .flatMap { $0.message } switch latestCommitMessage { case .Success(let message): println(message) case .Failure(let error): println(error.localizedDescription) } -
let url = NSURL( fileURLWithPath: "/Users/modocache/Gift")! let latestCommitMessage = openRepository(url)
.flatMap { $0.headReference } .flatMap { $0.commit } .flatMap { $0.message } switch latestCommitMessage { case .Success(let message): println(message) case .Failure(let error): println(error.localizedDescription) } -
openRepository(url) .flatMap { $0.index } .flatMap { $0.add() } .flatMap
{ $0.writeTree() } .flatMap { $0.commit("This is bananas!") } git commit --all --message "this is bananas" -
openRepository(url) .flatMap { $0.index } .flatMap { $0.add() } .flatMap
{ $0.writeTree() } .flatMap { $0.commit("This is bananas!") } git commit --all --message "this is bananas" -
openRepository(url) .flatMap { $0.index } .flatMap { $0.add() } .flatMap
{ $0.writeTree() } .flatMap { $0.commit("This is bananas!") } git commit --all --message "this is bananas" -
openRepository(url) .flatMap { $0.index } .flatMap { $0.add() } .flatMap
{ $0.writeTree() } .flatMap { $0.commit("This is bananas!") } git commit --all --message "this is bananas" -
openRepository(url) .flatMap { $0.index } .flatMap { $0.add() } .flatMap
{ $0.writeTree() } .flatMap { $0.commit("This is bananas!") } git commit --all --message "this is bananas" -
let entryCountResult = openRepository(url) .flatMap { $0.index } .map {
$0.entryCount } git status --staged | wc -l -
let entryCountResult = openRepository(url) .flatMap { $0.index } .map {
$0.entryCount } git status --staged | wc -l -
let entryCountResult = openRepository(url) .flatMap { $0.index } .map {
$0.entryCount } git status --staged | wc -l -
let entryCountResult = openRepository(url) .flatMap { $0.index } .map {
$0.entryCount } git status --staged | wc -l -
let entryCountResult = openRepository(url) .flatMap { $0.index } .map {
$0.entryCount } git status --staged | wc -l -
let entryCountResult = openRepository(url) .flatMap { $0.index } .map {
$0.entryCount } git status --staged | wc -l -
let entryCountResult = openRepository(url) .flatMap { $0.index } .map {
$0.entryCount } git status --staged | wc -l -
enum Result<SuccessType, ErrorType> { func map<NewType>(transform: SuccessType -> NewType) ->
Result<NewType, ErrorType> func flatMap<NewType>( transform: SuccessType -> Result<NewType, ErrorType>) -> Result<NewType, ErrorType> } -
enum Result<SuccessType, ErrorType> { func map<NewType>(transform: SuccessType -> NewType) ->
Result<NewType, ErrorType> func flatMap<NewType>( transform: SuccessType -> Result<NewType, ErrorType>) -> Result<NewType, ErrorType> } -
enum Result<SuccessType, ErrorType> { func map<NewType>(transform: SuccessType -> NewType) ->
Result<NewType, ErrorType> func flatMap<NewType>( transform: SuccessType -> Result<NewType, ErrorType>) -> Result<NewType, ErrorType> } -
enum Result<SuccessType, ErrorType> { func map<NewType>(transform: SuccessType -> NewType) ->
Result<NewType, ErrorType> func flatMap<NewType>( transform: SuccessType -> Result<NewType, ErrorType>) -> Result<NewType, ErrorType> } -
enum Result<SuccessType, ErrorType> { func map<NewType>(transform: SuccessType -> NewType) ->
Result<NewType, ErrorType> func flatMap<NewType>( transform: SuccessType -> Result<NewType, ErrorType>) -> Result<NewType, ErrorType> } -
enum Result<SuccessType, ErrorType> { func map<NewType>(transform: SuccessType -> NewType) ->
Result<NewType, ErrorType> func flatMap<NewType>( transform: SuccessType -> Result<NewType, ErrorType>) -> Result<NewType, ErrorType> } -
enum Result<SuccessType, ErrorType> { func map<NewType>(transform: SuccessType -> NewType) ->
Result<NewType, ErrorType> func flatMap<NewType>( transform: SuccessType -> Result<NewType, ErrorType>) -> Result<NewType, ErrorType> } -
let entryCountResult = openRepository(url) .flatMap { $0.index } .map {
$0.entryCount } git status --staged | wc -l