can we distinguish functions with lambda arguments by lambda’s return type?

Actually, you solution is rather elegant. I would only suggest to inline CR generic parameter and capture when subject in a variable:

fun <Type> CachableResponse.timeout(timeLambda: CachableResponse.() -> Type) =
    when (val it = timeLambda()) {
        is String -> timeout(it)
        is Date -> timeout(it)
        is Long -> timeout(it)
        else -> this

In this case, though, the developer won’t have any clue what its lambda will have to return without reading the description or checking the source code.

IDE comes to the rescue:

enter image description here enter image description here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top