• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Taming Errors with FunctionalKit
 

Taming Errors with FunctionalKit

on

  • 2,509 views

Introducing Option and Either in Objective-C.

Introducing Option and Either in Objective-C.

Statistics

Views

Total Views
2,509
Views on SlideShare
2,494
Embed Views
15

Actions

Likes
3
Downloads
30
Comments
1

2 Embeds 15

http://www.praytothemachine.com 12
http://www.slideshare.net 3

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • The code on slide 8 is incorrect. By documented convention, a method which returns an NSError by reference is not required or expected to clear the error on success. Therefore, the result of if (!error) is undefined on success. It should be if (val).
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

Taming Errors with FunctionalKit Taming Errors with FunctionalKit Presentation Transcript

  • taming errors with FunctionalKit Tuesday 24 March 2009
  • Error handling in Obj-C ‣ nil ‣ NSError ‣ @throw, and others? Tuesday 24 March 2009
  • nil Tuesday 24 March 2009
  • ‣ MyObject *object = [dictionary objectForKey:@”key”]; // Object might be nil. ‣ [object doSomething] => nil Tuesday 24 March 2009
  • Pros Easy to ignore Safe messaging Tuesday 24 March 2009
  • Cons Easy to ignore ‘Safe’ messaging Variadic functions (nil termination) Not in the Type - When do you get nils? You don’t have to think about it No error message Tuesday 24 March 2009
  • NSError Tuesday 24 March 2009
  • ‣ NSError *error; ‣ id val = [something withObject:object error:&error]; ‣ if (!error) { ... } else { ... } ‣ Or just use val directly. Tuesday 24 March 2009
  • Pros Easy to ignore Failure messages Clear to the API consumer Tuesday 24 March 2009
  • Cons (Arguably) dirtier code (Arguably) dirtier selectors I don’t like output parameters Mutable state Tuesday 24 March 2009
  • So... Tuesday 24 March 2009
  • Fancy Functional Types Tuesday 24 March 2009
  • nil → Option Tuesday 24 March 2009
  • Option - A better nil It fails when messaged explicit handling Tuesday 24 March 2009
  • single value array Tuesday 24 March 2009
  • Some value Tuesday 24 March 2009
  • Or none. Tuesday 24 March 2009
  • Getting an Option ‣ + (FKOption *)fromNil:(id)maybeNil; ‣ + (FKOption *)fromNil:(id)maybeNil ofType:(Class)cls; ‣ + (FKOption *)some:(id)value; ‣ + (FKOption *)none; Tuesday 24 March 2009
  • Using an Option ‣ - @property (readonly) BOOL isSome; ‣ - @property (readonly) BOOL isNone; ‣ - @property (readonly) id some; ‣ - (id)orSome:(id)some; Tuesday 24 March 2009
  • The Code Tuesday 24 March 2009
  • (value|NSError) → Either Tuesday 24 March 2009
  • Getting an Either ‣ + (FKEither *)eitherWithRight:(id)value; ‣ + (FKEither *)eitherWithLeft:(id)value; Tuesday 24 March 2009
  • Using an Either ‣ @property (readonly) FKRightProjection *right; ‣ @property (readonly) FKLeftProjection *left; ‣ @property (readonly) BOOL isLeft; ‣ @property (readonly) BOOL isRight; Tuesday 24 March 2009
  • Using a Left/Right ‣ @property (readonly) id *value; ‣ @property (readonly) id valueOrOther:(id)value; ‣ - (id)valueOrFailWithMessage:(NSString *)message; Tuesday 24 March 2009
  • ‘Wrong’ on Left ‘Right’ on Right Tuesday 24 March 2009
  • The Code Tuesday 24 March 2009
  • Is it better? Tuesday 24 March 2009
  • Still have to extract. Can still be noisy. Tuesday 24 March 2009
  • List of As map:: (a→b)→[a]→[b] List of Bs Function from A to B Tuesday 24 March 2009
  • Mapping with Option ‣ - (FKOption *)mapWithSelector:(SEL)selector; ‣ - (FKOption *)mapWithSelector:(SEL)selector onObject: (id)object; Tuesday 24 March 2009
  • The Code Tuesday 24 March 2009
  • Mapping with Either ‣ - (FKEither *)mapWithSelector:(SEL)selector; ‣ - (FKEither *)mapWithSelector:(SEL)selector onObject: (id)object; Tuesday 24 March 2009
  • The Code Tuesday 24 March 2009
  • The Code OR OR Tuesday 24 March 2009
  • By the way... Tuesday 24 March 2009
  • Option is a Monad. Tuesday 24 March 2009
  • Either is a Monad too. Tuesday 24 March 2009
  • FunctionalKit http://github.com/mogeneration/functionalkit Tuesday 24 March 2009
  • FunctionalKit ‣ FKOption ‣ FKEither ‣ FKFunction (a->b) ‣ FKEffect (a -> Nothing) ‣ FKP* ‣ FKNewtype ‣ FKMacros Tuesday 24 March 2009
  • resources http://github.com/mogeneration/ functionalkit http://www.qconlondon.com/london-2009/ presentation/Null+References:+The+Billion +Dollar+Mistake Tuesday 24 March 2009