Ulrich Schuster
10/11/2024, 7:57 AMforEachAccumulating
instead of mapOrAccumulate
when I am only interested in the errors, there does not seem to be a similarly complementarey function to zipOrAccumulate
, is there?
What I suppose I need is something along those lines:
fun RaiseAccumulate<MyError>.ensureIsValid(myIdString: String) =
ensureAccumulate( // this function does not exist
{ ensure( someCondition1(myIdString) ) { IncorrectCondition1(myIdString) } },
{ ensure( Dsomecondition2(myIdString) ) { IncorrectCondition2(myIdString) } }
)
Alejandro Serrano.Mena
10/11/2024, 8:11 AMzipOrAccumulate(
{ ensure(...) }
{ ensure(...) }
) { }
however, in the 2.0 to be released soon, you've be able to write much nicer code
accumulate {
ensureAccumulating(thing1) { ... }
ensureAccumulating(thing2) { ... }
}
Riccardo Cardin
10/14/2024, 5:09 AMensureAccumulating
function? I mean, in case of an error, you need to return something to let the execution proceedAlejandro Serrano.Mena
10/14/2024, 7:04 AMUnit
Riccardo Cardin
10/14/2024, 7:07 AMzipOrAccumulate
then. You can’t return a valid object as the return of the accumulate
function. Did I get it wrong?Alejandro Serrano.Mena
10/14/2024, 7:10 AMaccumulate {
val name by validateName(rawName).bindOrAccumulate()
val age by validateAge(rawAge).bindOrAccumulate()
Person(name, age)
}
Riccardo Cardin
10/14/2024, 7:13 AMAlejandro Serrano.Mena
10/14/2024, 7:16 AMaccumulate {
val name: Value<Name> = validateName(rawName).bindOrAccumulate()
val age: Value<Age> = validateAge(rawAge).bindOrAccumulate()
Person(name.getValue(), age.getValue())
}
during the bindOrAccumulate
(or any other accumulation function) we simply accumulate any errors in a temporary list. In the first call to getValue()
we check that list, and it's there's something, we just raise the corresponding set of errors.
unfortunately this is something which drinks a lot from Kotlin's delegated properties feature; I'm sure you can transpose it to Swift, but I'm not so sure about other languages...Riccardo Cardin
10/14/2024, 7:19 AMbut I’m not so sure about other languages...In fact, I was thinking about how to transpose it into Scala 😅 By the way, big shout-out 👏