Is there a way to capture the index of the errors ...
# arrow
b
Is there a way to capture the index of the errors in
mapOrAccumulate
?
y
Maybe something like:
Copy code
list.withIndex().mapOrAccumulate { (index, element) ->
  withError({ e: MyErrorType -> e to index }) {
    
  }
}
b
so you have to store it in the error type? i was wondering if i could avoid modifying the error type
y
How do you expect to use the error indices? I imagine you'd have to modify the error type regardless, no?
b
i guess i could create a new wrapper error with index
y
Exactly yeah, and to an extent, that's what my solution does, but it just uses a
Pair
b
oh I see, you're not using an explicit error type
y
Oh yeah, the
MyErrorType
here is just whatever error that your code already throws
b
i mean, using pair results in a
Either<Any, T>
instead of
Either<ErrorType, T>
y
It shouldn't tho. Do you have a code snippet maybe? I think accidentally you're using the
mapOrAccumulate
receiver instead of the
withError
one
b
i was returning a pair in one spot and an error in another, so it was inferring
Any
, i just wrapped it in a new error type so I could use my error interface
this is what i have atm:
Copy code
ranges.withIndex().mapOrAccumulate { (idx, range) ->
            val low = withError({ IndexedError(idx, it) }) { range.first.parseFreq() }
            val high = withError({ IndexedError(idx, it) }) { range.second.parseFreq() }
            ensure(low.valueInHz < high.valueInHz) {
                IndexedError(idx, InvalidInput("low value must be lower than high value"))
            }
            low to high
        }
1
i guess i could have also used
IndexedValue<T>