Just published an article about "How to use Arrows...
# feed
p
Just published an article about "How to use Arrows Either for exception handling in your application" https://medium.com/@inzuael/how-to-use-arrows-either-for-exception-handling-in-your-application-a73574b39d07
c
Hey! The article is quite good. We're building a library to make this easier: https://github.com/arrow-kt/arrow-exact. It's still experimental, but I think it's promising :)
Also, you can use
bind
instead of `flatMap`in a lot of cases to simplify the code. For example, these two snippets do exactly the same thing:
Copy code
fun addNewUser(user: UserDto): Either<Failure, User> {
    return user.toUser().flatMap {
        userRepository.save(it)
    }
}
Copy code
fun addNewUser(user: UserDto) = either {
    userRepository.save(
        user.toUser().bind()
    )
}
In this small example, the difference is slight, but it allows to avoid the nested lambda, thus reducing indentation. It also composes much better.
p
Thanks for your valuable feedback. I just started with Either few weeks ago and I'm still in the exploration phase. There is a lot possible and I need to find the best working ways. I will have a look on your suggestions
👍 1
Do have the correct return type I need to call bind() twice
Copy code
override fun addNewUser(user: UserDto): Either<Failure, User> = either {
        userRepository.save(user.toUser().bind()).bind()
    }
The
arrow-exact
library gives me the posibility to create value classes that are returning an
Exact
result type depending of valid/invalid input. Out of this I can compose more complex objects. This solves the problem of validation depends on single properites. But for me the problem remains that I maybe have validations which include multiple properties. So for this I still need a workaround in case I want to use a data class. In this case with
Exact
I also need to use an object type containing multiple properties as raw input. Something like:
Copy code
data class TopicDto(
    val id: Identity,
    val name: String,
    val category: String
)

data class Topic(
    val id: Identity,
    val name: String,
    val category: String,
) {
    companion object : Exact<TopicDto, Topic> {
        override fun Raise<ExactError>.spec(raw: TopicDto): Topic {
            ensure(raw.name.isNotEmpty())
            ensure(raw.category.length > 5)
            return Topic(
                id = raw.id,
                name = raw.name,
                category = raw.category
            )
        }

    }
}
c
Ah, that's true. Can you create an issue so we can discuss this with the others?
p
For what case do you mean? Using exact a data class having multiple properties (without definition of an extra typ for it)?
c
Yeah, that would be an interesting feature. I wonder if the solution would be to create an Exact instance for each field and compose them
p
Okay, I will try to explain my requirement.