Slide 1

Slide 1 text

Flexible API in Scala Jun Tomioka (M3, inc.)

Slide 2

Slide 2 text

M3, Inc. Jun Tomioka Twitter: @jooohn1234 Github: jooohn Love: Our very first baby Had great paternity leave!

Slide 3

Slide 3 text

How do you write your HTTP server interface?

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

http4s

Slide 7

Slide 7 text

http4s ● A minimal, idiomatic Scala interface for HTTP ● Scala’s answer to Ruby’s Rack ● Well designed “typeful” “functional” “streaming” library

Slide 8

Slide 8 text

Let’s see how http4s offers flexible APIs

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Basically, this is just a Request => F[Response]

Slide 12

Slide 12 text

Request => F[Response]

Slide 13

Slide 13 text

Scala’s functionality / design pattern ● Higher kinded type parameters ● Type Classes

Slide 14

Slide 14 text

Type parameters ● Parameterized type like Java’s Generics

Slide 15

Slide 15 text

Higher kinded type parameters ● Type parameters which expect higher kinded types. ○ F[_] ● Generalize “any contextual types” ○ Option / Future / Try / etc

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Type Classes ● Design pattern for ad-hoc polymorphism ● Generalize functionality which can be implemented later.

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

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` ?

Slide 24

Slide 24 text

Type Classes with higher kinded types ● Generalize operations for any contextual value. ● Powerful combination!

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

http4s

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

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.

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

“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.

Slide 39

Slide 39 text

Thanks!