Extension function literals


#1

Is it somehow possible to do like:

    val isEvenOrSexy: Int.(isSexy: Boolean) -> Boolean = {
        this % 2 == 0 || isSexy
    }

?

i saw something like that on some blog, but somehow i cannot make it working. … I always get unresolved reference when using isSexy argument…
Surely we can do this with no doubt:

    fun Int.sexyOrEven(isSexy: Boolean): Boolean {
        return this % 2 == 0 || isSexy
    }

Thanks in advance :slight_smile:


#2

Put the parameter name after the open bracket.

    val isEvenOrSexy: Int.(isSexy: Boolean) -> Boolean = { isSexy ->
        this % 2 == 0 || isSexy
    }

#3

thanks, i was missing that second parameters part,
bonus: for single parameter u can do it also like:

val isEvenOrSexy: Int.(isSexy: Boolean) -> Boolean = {
    this % 2 == 0 || it
}

#4

Why you prefer extension lambda property with single argument?
For val it looks exactly like extension method. Is it some kind of hack to overcome android method count limit?


#5

to be honest I am just testing new features ( to me) of Kotlin, so I don’t have in depth knowledge about how it affects total number of methods yet. I was just trying to make it work for fun