Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fun with Types

Fun with Types

Talk given at The Developers Conference in Sao Paulo, 2014 in the Scala track

22cac40ebe7d9e1e7854ea9808debd7b?s=128

Breno Ferreira

August 08, 2014
Tweet

Transcript

  1. FUN WITH TYPES Created by / Breno Ferreira @breno_ferreira

  2. IS YOUR CODE EXPLICIT?

  3. FOR EXAMPLE d e f d i v ( x

    : I n t , y : I n t ) = x / y
  4. WHAT HAPPENS WHEN? d i v ( 2 , 0

    )
  5. java.lang.ArithmeticException: / by zero

  6. HIDDEN SIDE-EFFECTS

  7. WHAT ABOUT THIS ONE? ( ) = > T

  8. A SIMPLE GETTER! IT CAN'T GO WRONG!

  9. Getters are used in Objects. Objects cause Side-Effects!

  10. FOR EXAMPLE: v a l s j o b s

    : ( ) = > S t r i n g s j o b s ( ) / / : " A p p l e I " s j o b s ( ) / / : " A p p l e I I " s j o b s ( ) / / : " M a c i n t o s h ! " s j o b s ( ) / / : " ✗"
  11. SO: ( ) = > T r y [ T

    ]
  12. FOR EXAMPLE: v a l s j o b s

    : ( ) = > S t r i n g s j o b s ( ) / / : " i P h o n e " s j o b s ( ) / / : " i P a d " s j o b s ( ) / / : " i C l o u d " s j o b s ( ) / / : " ✝"
  13. MAKING EVERYTHING EXPLICIT: ( ) = > T r y

    [ O p t i o n [ T ] ]
  14. WHAT ABOUT A GETTER OF GETTERS? ( ) = >

    ( ) = > T r y [ O p t i o n [ T ] ]
  15. EXTRACT TO INTERFACE t r a i t I t

    e r a b l e [ T ] { g e t I t e r a t o r : I t e r a t o r [ T ] } t r a i t I t e r a t o r [ T ] { h a s N e x t : B o o l e a n n e x t : T }
  16. WHAT IF IT'S ASYNC?

  17. GETTER ( ) = > T r y [ T

    ]
  18. ASYNC GETTER ( ( T r y [ T ]

    ) = > ( ) ) = > ( )
  19. BASICALLY, A FUTURE t r a i t F u

    t u r e [ T ] { o n S u c c e s s : T = > ( ) o n F a i l u r e : E x c e p t i o n = > ( ) }
  20. WHAT ABOUT MANY ASYNC VALUES?

  21. EASY: ( ( T r y [ I t e

    r a b l e [ T ] ] ) = > ( ) ) = > ( )
  22. BETTER: ( ( T r y [ O p t

    i o n [ T ] ] ) = > ( ) ) = > ( )
  23. OBSERVABLE/OBSERVER t r a i t O b s e

    r v a b l e [ T ] { s u b s c r i b e ( o b s e r v e r : O b s e r v e r [ T ] ) } t r a i t O b s e r v e r [ T ] { o n N e x t ( v a l u e : T ) o n E r r o r ( e r r o r : E x c e p t i o n ) o n C o m p l e t e d ( ) }
  24. FOUR BASIC EFFECTS IN COMPUTING: One Many Sync T /

    Option[T] Iterable[T] Async Future[T] Observable[T]
  25. Demo Code

  26. Q&A

  27. THANK YOU! http://brenoferreira.github.io/TDC14-JS @breno_ferreira