Slide 1

Slide 1 text

REVISITING TYPES IN KOTLIN “PACO” ESTEVEZ

Slide 2

Slide 2 text

FROM CLASSES TO TYPES EAT YOUR VEGGIES

Slide 3

Slide 3 text

CLASSES ENCAPSULATE DATA AND BEHAVIOUR. Classic OOP

Slide 4

Slide 4 text

DATA

Slide 5

Slide 5 text

DATA CHALLENGES ▸ Beans and not Beans ▸ Immutability ▸ Nullables and validation ▸ equals(), hashCode(), and toString()

Slide 6

Slide 6 text

BEHAVIOUR

Slide 7

Slide 7 text

HERE BE MONSTERS BEHAVIOUR IN JAVA

Slide 8

Slide 8 text

BUT WHAT IS A TYPE ANYWAY? Someone at the back

Slide 9

Slide 9 text

WHAT IS A TYPE ▸ A finite set of encapsulated operations ▸ A representation of either value or behaviour ▸ Some types can be represented with classes, some need language support ▸ They can be checked at compile time, so sometimes they’re called proofs ▸ Remember this

Slide 10

Slide 10 text

PRIMITIVES No implicit type coercion. Boxing is hidden from you.

Slide 11

Slide 11 text

FUNCTIONS

Slide 12

Slide 12 text

FUNCTIONS IN KOTLIN ▸ Functions are runtime objects too! ▸ Create an instance using operator :: ▸ Define them using lambda nomenclature ▸ A function as last parameter to another function gets syntactic sugar ▸ Great for DSLs

Slide 13

Slide 13 text

COLLECTIONS AND COMBINATORS

Slide 14

Slide 14 text

COLLECTIONS IN KOTLIN ▸ Saner version of Java’s, and fully compatible ▸ Two flavours: mutable and immutable ▸ Operations with function parameters: map, filter, reduce… https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

DATA CLASSES

Slide 17

Slide 17 text

DATA CLASSES IN KOTLIN ▸ Synthetic accessors ▸ val for immutable, var for mutable ▸ Nullable types and Default values ▸ Named parameters ▸ data qualifier: all of the above plus equals(), hashCode() and toString()

Slide 18

Slide 18 text

OPEN CLASSES AND INTERFACES

Slide 19

Slide 19 text

HERE BE JAVA

Slide 20

Slide 20 text

ENUMS

Slide 21

Slide 21 text

ENUM CLASSES IN KOTLIN ▸ Similar to Java ▸ values ▸ ordinal position ▸ Implement Enum interface if you’re a generic abstractions kind of person ▸ Look into Kotlin’s reified generics when you get home

Slide 22

Slide 22 text

SEALED CLASSES

Slide 23

Slide 23 text

DOMAIN DRIVEN DESIGN WITH TYPES ▸ Separate data from behaviour

Slide 24

Slide 24 text

DOMAIN DRIVEN DESIGN WITH TYPES ▸ Separate data from behaviour ▸ Express execution flows as data

Slide 25

Slide 25 text

DOMAIN DRIVEN DESIGN WITH TYPES ▸ Separate data from behaviour ▸ Express execution flows as data ▸ Encode behaviour in types

Slide 26

Slide 26 text

DOMAIN DRIVEN DESIGN WITH TYPES ▸ Separate data from behaviour ▸ Express execution flows as data ▸ Encode behaviour in types ▸ Make illegal states unrepresentable

Slide 27

Slide 27 text

DOMAIN DRIVEN DESIGN WITH TYPES ▸ Separate data from behaviour ▸ Express execution flows as data ▸ Encode behaviour in types ▸ Make illegal states unrepresentable ▸ Push validation to edges

Slide 28

Slide 28 text

PART TWO

Slide 29

Slide 29 text

SEALED CLASSES IN KOTLIN ▸ Closed inheritance ▸ Accessing fields and methods requires dereferencing using when() and is -> ▸ The compiler checks that all cases are handled! ▸ Data classes and implement interfaces (1.1) ▸ Type alias (1.1)

Slide 30

Slide 30 text

MAKE THE COMPILER WORK FOR YOU! Me

Slide 31

Slide 31 text

WHAT IF I CAN’T USE KOTLIN? JAVA 8 ▸ JavaSealedUnions JAVA 6 ▸ RxSealedUnions SCALA ▸ You should be on stage pacoworks.com/ @fe_hudl github.com/pakoito tinyurl.com/KMobi16