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

Kotlin and ADT

Kotlin and ADT

"We'll talk about what algebraic data types are and how you can start using them today. We are not going to dive into any frameworks, so it does not matter whether you are a frontend or backend developer - it will be equally useful."
by Dmitry Zaytsev
presented on August 22, 2017 at @car2go

Kotlin User Group Hamburg

August 22, 2017
Tweet

More Decks by Kotlin User Group Hamburg

Other Decks in Programming

Transcript

  1. What is an ADT? Algebraic Data Type Type which is

    represented by several other types 5 / 37
  2. What is an ADT? Algebraic Data Type Type which is

    represented by several other types enum is also an ADT 8 / 37
  3. class OperationStatus<T> { public final Stage stage; public final T

    result; public final String errorMessage; enum Stage { SUCCESS, FAILURE, IN_PROGRESS } } 10 / 37
  4. class OperationStatus<T> { public final Stage stage; public final T

    result; public final String errorMessage; enum Stage { SUCCESS, FAILURE, IN_PROGRESS } } 13 / 37
  5. sealed class OperationStatus<T> { object InProgress<T> : OperationStatus<T>() data class

    Failure<T>(message: String) : OperationStatus()<T> } 17 / 37
  6. sealed class OperationStatus<T> { object InProgress<T> : OperationStatus<T>() data class

    Failure<T>(message: String) : OperationStatus()<T> data class Success<T>(result: T) : OperationStatus<T>() } 18 / 37
  7. fun showOperationStatus(status: OperationStatus<String>) { when (status) { is InProgress ->

    showProgress() is Success -> showSuccess(status.result) } } 20 / 37
  8. fun showOperationStatus(status: OperationStatus<String>) { when (status) { is InProgress ->

    showProgress() is Success -> showSuccess(status.result) } } 21 / 37
  9. fun showOperationStatus(status: OperationStatus<String>) { when (status) { is InProgress ->

    showProgress() is Success -> showSuccess(status.result) } } 22 / 37
  10. fun showOperationStatus(status: OperationStatus<String>) { when (status) { is InProgress ->

    showProgress() is Success -> showSuccess(status.result) } } Something is missing... 23 / 37
  11. fun showOperationStatus(status: OperationStatus<String>): Unit { return when (status) { //

    does not compile is InProgress -> showProgress() is Success -> showSuccess(status.result) } } 24 / 37
  12. fun showOperationStatus(status: OperationStatus<String>) { return when (status) { is InProgress

    -> showProgress() is Success -> showSuccess(status.result) is Failure -> showError(status.message) } } 25 / 37
  13. sealed class ReservationState { object NoReservation : ReservationState() data class

    Reserving(vehicleId: String) : ReservationState() data class Cancelling(vehicleId: String) : ReservationState() data class Reserved(vehicleId: String) : ReservationState() sealed class Failure : ReservationState() { object NoInternet : Failure() object NotLoggedIn : Failure() object NotAllowedToDrive : Failure() } } 27 / 37
  14. // Either emits value or emits error fun getReservedVehicle(): Observable<Vehicle>

    // Will crash if error is emitted getReservedVehicle() .subscribe { /* do stuff */ } 28 / 37
  15. // Either emits value or emits error fun getReservedVehicle(): Observable<OperationStatus<Vehicle>>

    // Will not crash, error handled as a value getReservedVehicle() .subscribe { handleReservedVehicleResult(it) } 29 / 37
  16. fun handleReservedVehicleResult(result: ReservationStatus) { return when(result) { is NoReservation ->

    showNoReservation() is Reserving -> showProgressForVehicle(result.vehicleId) // the rest of the types } } 30 / 37
  17. To wrap it up Represent one of several possible states.

    Forces you to handle all cases. 32 / 37
  18. To wrap it up Represent one of several possible states.

    Forces you to handle all cases. Can be nested. 33 / 37
  19. To wrap it up Represent one of several possible states.

    Forces you to handle all cases. Can be nested. Works great with smart cast. 34 / 37
  20. To wrap it up Represent one of several possible states.

    Forces you to handle all cases. Can be nested. Works great with smart cast. Works great with RxJava 35 / 37
  21. To wrap it up Represent one of several possible states.

    Forces you to handle all cases. Can be nested. Works great with smart cast. Works great with RxJava Not a replacement for every model. 36 / 37