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

Type Safety in an Unsafe World with Kotlin

Cody Engel
February 02, 2021

Type Safety in an Unsafe World with Kotlin

Cody Engel

February 02, 2021
Tweet

More Decks by Cody Engel

Other Decks in Technology

Transcript

  1. Type Safety in an Unsafe World with Kotlin Cody Engel,

    Staff Software Engineer @ PayPal
  2. Well Typed It may crash, but that is by design

    in Kotlin. Type Safety by Michael Hicks - http://bit.ly/ple-type-safety
  3. Well Typed, Continued This is by design in Ruby and

    may also crash. Type Safety by Michael Hicks - http://bit.ly/ple-type-safety
  4. Cody Engel, Type Safety in an Unsafe World with Kotlin

    “Some languages have stricter behavior than others.”
  5. What is Kotlin? It is a strict, yet concise programming

    language. Photo: Marc Reichelt via Unsplash
  6. is_admin Our permissions class was never safer! However this is

    how we would model that information in Kotlin.
  7. is_admin Okay, technically this isn’t too bad. Although, it is

    not the best way to model a boolean with JSON.
  8. is_admin Extension functions to the rescue. This could also be

    an extension property, but extension functions look better in Keynote
  9. is_admin Strings can also represent numeric values which can represent

    booleans. I was pretty excited to make use of extension functions, so it was fine.
  10. is_admin Unfortunately String already has a toBoolean function. And that

    function is for converting “true” and “false” into a boolean
  11. is_admin I can’t recall if this was actually possible with

    the API. However there is a good chance it could have been possible based on customer configurations.
  12. is_admin Fortunately, it isn’t too difficult to add. Nonetheless, it’d

    be great if this wasn’t required in the first place
  13. is_admin Updating our extension function to be on a nullable

    Any works. Since we type cast on non-null types, the else branch catches our null case.
  14. Making The World a Safer Place How to design safer

    APIs. Photo: Matthew Rumph via Unsplash
  15. Let’s build some great and safe APIs. • YouTube -

    bit.ly/cody-yt • Twitter - bit.ly/cody-twitter • Medium - bit.ly/cody-medium Thanks!