1
votes

I have following in my kotlin code:

return Failsafe.with<Boolean>(retryPolicy)
        // Here I want fall back to false
        .withFallback { -> false} // Does not work
        .get { ->
            publish(event)
            true
        }

Kotlin compiler complains that:

Error:(89, 14) Kotlin: Overload resolution ambiguity:

public final fun withFallback(p0: (() -> Boolean!)!): SyncFailsafe! defined in net.jodah.failsafe.SyncFailsafe

public final fun withFallback(p0: (() -> Unit)!): SyncFailsafe! defined in net.jodah.failsafe.SyncFailsafe

But this way it works:

val fallback: () -> Boolean = { false }

return Failsafe.with<Boolean>(retryPolicy)
        // Here I want fall back to false
        .withFallback(fallback) // Works
        .get { ->
            publish(event)
            true
        }

Could somebody explain me why it does not work with lambda literal outside brackets? And how to make it work with lambda inline?

1
Try to remove the ->, they are only necessary when you are defining parameters in front of itmsrd0

1 Answers

1
votes

If you write

val fallback: () -> Unit = { -> false }

you'll see it compiles as well (it just discards the result). So { -> false} can be used for both types () -> Boolean and () -> Unit and the compiler doesn't know which you want.

So to use a lambda, specify the desired type there:

.withFallback({ -> false} as () -> Boolean)

or create an extension function without an overload, e.g.:

fun <T, F> FailsafeConfig<T, F>.withFallbackT(fallback: () -> T) = withFallback(fallback)

return Failsafe.with<Boolean>(retryPolicy)
    .withFallbackT { -> false}...