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

Revisiting types in Kotlin

pakoito
October 22, 2016

Revisiting types in Kotlin

Snippets
=======

Collections and functions:
https://gist.github.com/pakoito/0fd5d1bd6c1523ad965b52cc7b592433

Domain Modeling with Sealed classes:
https://gist.github.com/pakoito/0b942c6f68441b76756a7a761cb9272d

VIDEO
=====
TBD

pakoito

October 22, 2016
Tweet

More Decks by pakoito

Other Decks in Programming

Transcript

  1. DATA CHALLENGES ▸ Beans and not Beans ▸ Immutability ▸

    Nullables and validation ▸ equals(), hashCode(), and toString()
  2. 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
  3. 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
  4. 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/
  5. 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()
  6. ENUM CLASSES IN KOTLIN ▸ Similar to Java ▸ values

    ▸ ordinal position ▸ Implement Enum<T> interface if you’re a generic abstractions kind of person ▸ Look into Kotlin’s reified generics when you get home
  7. DOMAIN DRIVEN DESIGN WITH TYPES ▸ Separate data from behaviour

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

    ▸ Express execution flows as data ▸ Encode behaviour in types ▸ Make illegal states unrepresentable
  9. 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
  10. 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)
  11. 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