From 687c4c9d7f1a246ed30c5d2bbd226624259def0b Mon Sep 17 00:00:00 2001 From: Michael Bull Date: Wed, 24 Jan 2018 12:58:26 +0000 Subject: [PATCH] Omit "this" prefix from method references https://kotlinlang.org/docs/reference/whatsnew12.html#support-for--foo-as-a-shorthand-for-thisfoo --- README.md | 2 +- .../result/example/service/CustomerService.kt | 23 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3bf1813..8387575 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ having sufficient privileges, and the command executing the associated action. ```kotlin tokenize(command.toLowerCase()) - .andThen(this::findCommand) + .andThen(::findCommand) .andThen { cmd -> checkPrivileges(loggedInUser, cmd) } .andThen { execute(user = loggedInUser, command = cmd, timestamp = LocalDateTime.now()) } .mapBoth( diff --git a/example/src/main/kotlin/com/github/michaelbull/result/example/service/CustomerService.kt b/example/src/main/kotlin/com/github/michaelbull/result/example/service/CustomerService.kt index 2412da4..55d1c43 100644 --- a/example/src/main/kotlin/com/github/michaelbull/result/example/service/CustomerService.kt +++ b/example/src/main/kotlin/com/github/michaelbull/result/example/service/CustomerService.kt @@ -1,10 +1,16 @@ package com.github.michaelbull.result.example.service -import com.github.michaelbull.result.* +import com.github.michaelbull.result.Err +import com.github.michaelbull.result.Ok +import com.github.michaelbull.result.Result +import com.github.michaelbull.result.andThen import com.github.michaelbull.result.example.model.domain.Customer import com.github.michaelbull.result.example.model.domain.CustomerId import com.github.michaelbull.result.example.model.domain.DomainMessage import com.github.michaelbull.result.example.model.entity.CustomerEntity +import com.github.michaelbull.result.map +import com.github.michaelbull.result.mapBoth +import com.github.michaelbull.result.mapError import java.sql.SQLTimeoutException object CustomerService { @@ -12,7 +18,7 @@ object CustomerService { fun getAll(): Result, DomainMessage> { return Result.of(repository::findAll) - .mapError(this::exceptionToDomainMessage) + .mapError(::exceptionToDomainMessage) .andThen { result: Collection -> Ok(result.map { val customer = Customer.from(it) @@ -39,21 +45,24 @@ object CustomerService { private fun updateCustomer(entity: CustomerEntity, old: Customer, new: Customer) = Result.of { repository.update(entity) } .map { differenceBetween(old, new) } - .mapError(this::exceptionToDomainMessage) + .mapError(::exceptionToDomainMessage) private fun createCustomer(entity: CustomerEntity) = Result.of { repository.insert(entity) } - .mapError(this::exceptionToDomainMessage) .map { DomainMessage.CustomerCreated } + .mapError(::exceptionToDomainMessage) private fun findCustomer(customers: Collection, id: CustomerId): Result { val customer = customers.find { it.id == id } return if (customer != null) Ok(customer) else Err(DomainMessage.CustomerNotFound) } - private fun differenceBetween(old: Customer, new: Customer) = when { - new.email != old.email -> DomainMessage.EmailAddressChanged(old.email.address, new.email.address) - else -> null + private fun differenceBetween(old: Customer, new: Customer): DomainMessage.EmailAddressChanged? { + return if (new.email != old.email) { + DomainMessage.EmailAddressChanged(old.email.address, new.email.address) + } else { + null + } } private fun exceptionToDomainMessage(t: Throwable) = when (t) {