Son
10/19/2020, 4:29 PMfun fn1(x: Int): Boolean = x % 2 == 0
fun fn2(x: Int): Boolean { return x % 2 == 0 }
fun main () {
val listNum = listOf(1, 2, 3, 4, 5, 6)
val fn3: (Int) -> Boolean = { it % 2 == 0 }
println(listNum.filter(::fn1)) // compiles and outputs [2, 4, 6]
println(listNum.filter(::fn2)) // compiles and outputs [2, 4, 6]
println(listNum.filter(fn3)) // compiles and outputs [2, 4, 6]
// e: org.jetbrains.kotlin.util.KotlinFrontEndException: Exception while analyzing expression at (17,13)
println(listNum.filter(::fn3)) // Error
}
Razvan
10/19/2020, 4:34 PM::
references a function val fn3
declares a variable not a function so you can't use ::Son
10/19/2020, 4:37 PMRazvan
10/19/2020, 4:43 PMVampire
10/19/2020, 4:47 PMSon
10/19/2020, 4:48 PMVampire
10/19/2020, 4:49 PMwhy I could not do this (i.e. treating lambda as a function reference).Of course you can do this, or your second line would not work. Your problem is, that you already have the function reference in the variable fn3 and with
::fn3
you make a reference to the function reference, namely a reference to the variable fn3
.Except there is not icon on the bottom right and the image link is brokenWell, that's probably a second error you should report. 🙂
Son
10/19/2020, 4:49 PMVampire
10/19/2020, 4:49 PMRazvan
10/19/2020, 4:58 PM::fun3.get()
Unsupported [References to variables aren't supported yet]that's the way you should reference properties, but as this is a var is unsupported .. yet.
Son
10/19/2020, 5:46 PM