Lately I’ve been trying to create a small library ...
# arrow
c
Lately I’ve been trying to create a small library that could be public. My question is the following: When you have a function with optional parameters (these parameters are lambdas), which is better to pass an empty lambda or create several functions that make use of all its parameters?
For Example:
Copy code
fun <A, B> launchIOSafe(
    f: suspend () -> Either<A, B>,
    error: suspend (A) -> Unit = {},
    success: suspend (B) -> Unit = {},
): Job =
    launchMain {
        IO { f() }.fold(error, success)
    }
s
Yes, that makes sense. The only way to call it with only error is
launchIOSafe(error = { _: A -> })
. You can never call it with the trailing lambda syntax. In some cases you can avoid this, but not always. I think in this case this result in any nicer APIs. In fact, it may be even conflicting. Sometimes you can define APIs that are valid in their definition, but ambiguous on the call-site.
Copy code
fun <A, B> launchIOSafe(
    f: suspend () -> Either<A, B>,
    error: suspend (A) -> Unit = {},
    success: suspend (B) -> Unit
): Job =
    launchMain {
        IO { f() }.fold(error, success)
    }

fun <A, B> launchIOSafe(
    f: suspend () -> Either<A, B>,
    error: suspend (A) -> Unit
): Job =
    launchIOSafe(f, error) { }