arrow-kt / arrow

Λrrow - Functional companion to Kotlin's Standard Library
http://arrow-kt.io
Other
6.13k stars 442 forks source link

Allow specifying a more concrete Throwable type for retrying, alternative #3418

Closed serras closed 4 months ago

serras commented 4 months ago

This is an alternative approach to #3414, which I think gives us better ergonomics

The main changes are:

github-actions[bot] commented 4 months ago

Kover Report

File Coverage [68.21%]
arrow-libs/resilience/arrow-resilience/src/commonMain/kotlin/arrow/resilience/Schedule.kt 68.21%
Total Project Coverage 61.06%
serras commented 4 months ago

I think I got it!! Actually variants using E everywhere is all we need!

fun <E: Throwable, A> Schedule<E, *>.retry(...)

This works without the intermediate E: Throw: Throwable because of the contravariance of Schedule. That is, since E: Throwable, then Schedule<Throwable, ...>: Schedule<E, ...>, as needed for the function to apply. I think this gives great ergonomics, as shown by the tests:

nomisRev commented 4 months ago

But you can also use s.retry<IllegalStateException, _> { ... } if you want a more concrete type to be filtered than the one coming from s. Furthermore, we ensure that IllegalStateException is indeed a subtype of the input to s, as required.

That's really nice ergonomics, also I have really underused _. I need to used it more, would also be good to use _ more in the Arrow docs IMHO. It looks so much better.