Muhammad Talha
08/16/2022, 4:59 AMdnowak
08/18/2022, 2:13 PMfun sth(
dependency1: Dependency1,
dependency2: Dependency2,
...
param1: Param1,
param2, Param2,
...
)
To define contract we use type aliases. Your case would look like this in our codebase:
//API
typealias GetUser = (UserId) -> Either<StorageError, User>
//IMPL
fun getUseser(
dbClient: DbClient,
userId: UserId
): Either<StorageError, User> {
//access DB, handle data & errors
}
We use Spring for bunding the application, so somewhere in configuration we would have:
@Bean
fun getUserFun(dbClient: DbClient): GetUser = ::getUser.partially1(dbClient)
@Bean
fun someUseCaseFun(getUser: GetUser, ....): SomeUseCase = ::someUseCase.partially1(getUser)
We are quite happy with such approach.Either
and partially1
come of course from Arrow
🙂.Muhammad Talha
08/20/2022, 2:17 AMdnowak
08/22/2022, 11:25 AMResult
you can only define the right side (positive result) - the left is bound to Throwable
. So Result
is like Either<Throwable, T>
. In FP you should not work on exceptions -> every operation that may fail should return meaning full error which you can later handle or map to an error of “higher layer”. If some operation may fail we wrap it in Either.catch
and use mapLeft
to transform Throwable to custom error instance.Muhammad Talha
08/24/2022, 5:19 AM