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 doS...
Pros
                         Easy to ignore
                        Safe messaging




Tuesday 24 March 2009
Cons
                                      Easy to ignore
                                    ‘Safe’ messaging
           ...
NSError



Tuesday 24 March 2009
‣      NSError *error;

      ‣      id val = [something withObject:object error:&error];

      ‣      if (!error) { ... ...
Pros
                              Easy to ignore
                            Failure messages
                        Cle...
Cons
                            (Arguably) dirtier code
                          (Arguably) dirtier selectors
          ...
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...
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 ofT...
Using an Option



      ‣      - @property (readonly) BOOL isSome;

      ‣      - @property (readonly) BOOL isNone;

   ...
The Code




Tuesday 24 March 2009
(value|NSError) →
                      Either


Tuesday 24 March 2009
Getting an Either




      ‣      + (FKEither *)eitherWithRight:(id)value;

      ‣      + (FKEither *)eitherWithLeft:(id...
Using an Either



      ‣      @property (readonly) FKRightProjection *right;

      ‣      @property (readonly) FKLeftPr...
Using a Left/Right



      ‣      @property (readonly) id *value;

      ‣      @property (readonly) id valueOrOther:(id)...
‘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
                   Funct...
Mapping with Option



      ‣      - (FKOption *)mapWithSelector:(SEL)selector;

      ‣      - (FKOption *)mapWithSelect...
The Code




Tuesday 24 March 2009
Mapping with Either



      ‣      - (FKEither *)mapWithSelector:(SEL)selector;

      ‣      - (FKEither *)mapWithSelect...
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 -> N...
resources
           http://github.com/mogeneration/
                      functionalkit
      http://www.qconlondon.com/l...
Upcoming SlideShare
Loading in …5
×

Taming Errors with FunctionalKit

1,735 views

Published on

Introducing Option and Either in Objective-C.

Published in: Technology, News & Politics
1 Comment
3 Likes
Statistics
Notes
  • 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).
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
1,735
On SlideShare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
35
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide








































  • Taming Errors with FunctionalKit

    1. 1. taming errors with FunctionalKit Tuesday 24 March 2009
    2. 2. Error handling in Obj-C ‣ nil ‣ NSError ‣ @throw, and others? Tuesday 24 March 2009
    3. 3. nil Tuesday 24 March 2009
    4. 4. ‣ MyObject *object = [dictionary objectForKey:@”key”]; // Object might be nil. ‣ [object doSomething] => nil Tuesday 24 March 2009
    5. 5. Pros Easy to ignore Safe messaging Tuesday 24 March 2009
    6. 6. 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
    7. 7. NSError Tuesday 24 March 2009
    8. 8. ‣ NSError *error; ‣ id val = [something withObject:object error:&error]; ‣ if (!error) { ... } else { ... } ‣ Or just use val directly. Tuesday 24 March 2009
    9. 9. Pros Easy to ignore Failure messages Clear to the API consumer Tuesday 24 March 2009
    10. 10. Cons (Arguably) dirtier code (Arguably) dirtier selectors I don’t like output parameters Mutable state Tuesday 24 March 2009
    11. 11. So... Tuesday 24 March 2009
    12. 12. Fancy Functional Types Tuesday 24 March 2009
    13. 13. nil → Option Tuesday 24 March 2009
    14. 14. Option - A better nil It fails when messaged explicit handling Tuesday 24 March 2009
    15. 15. single value array Tuesday 24 March 2009
    16. 16. Some value Tuesday 24 March 2009
    17. 17. Or none. Tuesday 24 March 2009
    18. 18. Getting an Option ‣ + (FKOption *)fromNil:(id)maybeNil; ‣ + (FKOption *)fromNil:(id)maybeNil ofType:(Class)cls; ‣ + (FKOption *)some:(id)value; ‣ + (FKOption *)none; Tuesday 24 March 2009
    19. 19. Using an Option ‣ - @property (readonly) BOOL isSome; ‣ - @property (readonly) BOOL isNone; ‣ - @property (readonly) id some; ‣ - (id)orSome:(id)some; Tuesday 24 March 2009
    20. 20. The Code Tuesday 24 March 2009
    21. 21. (value|NSError) → Either Tuesday 24 March 2009
    22. 22. Getting an Either ‣ + (FKEither *)eitherWithRight:(id)value; ‣ + (FKEither *)eitherWithLeft:(id)value; Tuesday 24 March 2009
    23. 23. Using an Either ‣ @property (readonly) FKRightProjection *right; ‣ @property (readonly) FKLeftProjection *left; ‣ @property (readonly) BOOL isLeft; ‣ @property (readonly) BOOL isRight; Tuesday 24 March 2009
    24. 24. Using a Left/Right ‣ @property (readonly) id *value; ‣ @property (readonly) id valueOrOther:(id)value; ‣ - (id)valueOrFailWithMessage:(NSString *)message; Tuesday 24 March 2009
    25. 25. ‘Wrong’ on Left ‘Right’ on Right Tuesday 24 March 2009
    26. 26. The Code Tuesday 24 March 2009
    27. 27. Is it better? Tuesday 24 March 2009
    28. 28. Still have to extract. Can still be noisy. Tuesday 24 March 2009
    29. 29. List of As map:: (a→b)→[a]→[b] List of Bs Function from A to B Tuesday 24 March 2009
    30. 30. Mapping with Option ‣ - (FKOption *)mapWithSelector:(SEL)selector; ‣ - (FKOption *)mapWithSelector:(SEL)selector onObject: (id)object; Tuesday 24 March 2009
    31. 31. The Code Tuesday 24 March 2009
    32. 32. Mapping with Either ‣ - (FKEither *)mapWithSelector:(SEL)selector; ‣ - (FKEither *)mapWithSelector:(SEL)selector onObject: (id)object; Tuesday 24 March 2009
    33. 33. The Code Tuesday 24 March 2009
    34. 34. The Code OR OR Tuesday 24 March 2009
    35. 35. By the way... Tuesday 24 March 2009
    36. 36. Option is a Monad. Tuesday 24 March 2009
    37. 37. Either is a Monad too. Tuesday 24 March 2009
    38. 38. FunctionalKit http://github.com/mogeneration/functionalkit Tuesday 24 March 2009
    39. 39. FunctionalKit ‣ FKOption ‣ FKEither ‣ FKFunction (a->b) ‣ FKEffect (a -> Nothing) ‣ FKP* ‣ FKNewtype ‣ FKMacros Tuesday 24 March 2009
    40. 40. resources http://github.com/mogeneration/ functionalkit http://www.qconlondon.com/london-2009/ presentation/Null+References:+The+Billion +Dollar+Mistake Tuesday 24 March 2009

    ×