dimsuz
06/16/2023, 1:10 PM// state is a screen's state to render
RegisterCredentials(state.email.text, state.password.text)
.fold(
ifLeft = { errors ->
val emailError = (
Fold.nonEmptyList<RegisterCredentialsValidationError>() compose
RegisterCredentialsValidationError.invalidEmail compose
RegisterCredentialsValidationError.InvalidEmail.errors compose
Fold.nonEmptyList()
)
.firstOrNull(errors)
val passwordError = (
Fold.nonEmptyList<RegisterCredentialsValidationError>() compose
RegisterCredentialsValidationError.invalidPassword compose
RegisterCredentialsValidationError.InvalidPassword.errors compose
Fold.nonEmptyList()
)
.firstOrNull(errors)
state.copy {
ViewState.nullableEmailError set emailError
ViewState.nullablePasswordError set passwordError
ViewState.privacyPolicyErrorVisible set !state.privacyPolicyAccepted
}
},
ifRight = { state }
)
Alejandro Serrano Mena
06/16/2023, 3:10 PMFold.nonEmptyList<RegisterCredentialsValidationError>() compose
RegisterCredentialsValidationError.invalidEmail compose
RegisterCredentialsValidationError.InvalidEmail.errors compose
Fold.nonEmptyList()
you should be able to write
Every.nonEmptyList<RegisterCredentialsValidationError>().invalidEmail.errors.nonEmptyList()
dimsuz
06/16/2023, 3:30 PMnonEmptyList
from your example. It will take me some time to wrap my head around the types (not that familiar with Every yet). But here's the type I get:
val e: Every<NonEmptyList<UserDetailsValidationError>, NonEmptyList<NameValidationError>> =
Every.nonEmptyList<UserDetailsValidationError>().invalidName.errors
Nel<NameValidationError>
e.firstOrNull(errors).first()
of course, but maybe there's some more optics magic I can do to be less verbose?Alejandro Serrano Mena
06/19/2023, 6:56 AMindex
(https://apidocs.arrow-kt.io/arrow-optics/arrow.optics.dsl/--index--.html) can be used to access any element as Optional, so you could use index(0)
to access the first elementCons
(https://apidocs.arrow-kt.io/arrow-optics/arrow.optics.typeclasses/-cons/-companion/index.html) which is especifically for getting the first element of thingsEvery.nonEmptyList<UserDetailsValidationError>().invalidName.errors.index(Index.nonEmptyList(), 0)
dimsuz
06/19/2023, 1:37 PMval error = (Every.nonEmptyList<UserDetailsValidationError>()
.invalidName
.errors as Fold<NonEmptyList<UserDetailsValidationError>, NonEmptyList<NameValidationerror>>)
.index(Index.nonEmptyList(), 0)
.firstOrNull(errors)
Alejandro Serrano Mena
06/22/2023, 7:59 AMotherwise it didn’t compile due to overload ambiguitythis is one of the things we’ve fixed in Arrow 2.0, but we decided to not include it in 1.2.0 because it’s a breaking change
dimsuz
06/23/2023, 10:03 AM