https://kotlinlang.org logo
#akkurate
Title
# akkurate
r

Ronny Bräunlich

10/20/2023, 6:34 AM
Hello, I saw yesterday's annoucement of akkurate and took a quick glance at it. I was wondering why you've chosen to implement your own
ValidationResult
class instead of e.g. using Arrow's
Either
. I guess the latter would make for a better interoperability.
p

PoisonedYouth

10/20/2023, 6:58 AM
You can create extension functions for the mapping from
ValidationResult
to `Either`/`EitherNel` . Something like below:
Copy code
fun <T> ValidationResult<T>.toEitherNel(): EitherNel<Failure, T> =
    when (this) {
        is ValidationResult.Success<T> -> Either.Right(this.value)
        is ValidationResult.Failure -> {
            Either.Left(this.violations.map { Failure.ValidationFailure(it.message) }.toNonEmptyList())
        }
    }

fun <T> ValidationResult<T>.toEither(): Either<Failure, T> =
    when (this) {
        is ValidationResult.Success<T> -> Either.Right(this.value)
        is ValidationResult.Failure -> {
            Either.Left(Failure.ValidationFailure(this.violations.joinToString { it.message }))
        }
    }
r

Ronny Bräunlich

10/20/2023, 7:01 AM
I was wondering about the design decision
p

PoisonedYouth

10/20/2023, 7:06 AM
Isn't that forcing the user to have a dependency to Arrow Core?
r

Ronny Bräunlich

10/20/2023, 7:13 AM
Well, yes, but then you'd have a well tested implementation instead of the n-th own implementation of an Either
p

PoisonedYouth

10/20/2023, 7:14 AM
That's correct.
j

Johann Pardanaud

10/20/2023, 7:19 AM
The extra dependency is the reason, not everyone is using Arrow 😉 But I will provide soon an integration to cast the result to an Either, like the extension function @PoisonedYouth wrote.
👍 2