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

Flexible API in Scala

Flexible API in Scala

Explains how to write flexible APIs in Scala.

Jun Tomioka

May 11, 2018

More Decks by Jun Tomioka

Other Decks in Technology


  1. M3, Inc. Jun Tomioka Twitter: @jooohn1234 Github: jooohn Love: Our

    very first baby Had great paternity leave!
  2. http4s • A minimal, idiomatic Scala interface for HTTP •

    Scala’s answer to Ruby’s Rack • Well designed “typeful” “functional” “streaming” library
  3. Higher kinded type parameters • Type parameters which expect higher

    kinded types. ◦ F[_] • Generalize “any contextual types” ◦ Option / Future / Try / etc
  4. Type Classes • Design pattern for ad-hoc polymorphism • Generalize

    functionality which can be implemented later.
  5. Why type class? • Can extend existing class. • Different

    implementation for same class. ◦ intAddition / intMultiplication • Independent from an instance. ◦ What if you want `def empty[A]: A` ?
  6. Type Classes with higher kinded types • Generalize operations for

    any contextual value. • Powerful combination!
  7. Flexibility of type class • Taking F as type parameter,

    it allows us to choose which context to use. ◦ http4s basically expects any instance of Effect as F like IO • You can use your own instance of Effect ◦ DB access / API Call / Body parsing ◦ IO seems perfect choice though.
  8. Flexible API with Contextual type classes • Declare traits with

    type parameter F[_] ◦ Never know concrete type ◦ Use type class if any functionality is needed • Offers flexible API with ◦ Higher kinded types ◦ Type classes
  9. “Looks too much.” • If you think so, YOU’RE RIGHT.

    ◦ You may not have to write such APIs BY YOURSELF in your application code. ◦ Instead, USE it for your needs.