fork-handles / forkhandles

Foundational libraries for Kotlin
https://forkhandles.dev
Apache License 2.0
224 stars 28 forks source link

[result4k] added `resultFromCatching` for catching a specific type of exception #56

Closed MarcusDunn closed 8 months ago

MarcusDunn commented 8 months ago

resultFrom is a nice way to bridge exception throwing code with more controlled results, but catching every exception type is often not what one wants or you may know the exception type that will be thrown. resultFromCatching allow catching a subset of Exeption types.

I've had this extension function in my own code base for a bit now - It gets used fairly often (and is preferred over resultFrom) and I thought it may be wanted in the library itself.

Example

fun DateTimeFormatter.parseResult(dateString: String) = resultFromCatching<DateTimeParseException, _> { parse(dateString) }
daviddenton commented 8 months ago

Lgtm! :)

Remember to update the changelog to get full credit 😉

daviddenton commented 8 months ago

Non breaking, so it's a point release 😉