there's a method which takes a @FunctionalInterfac...
# announcements
g
there's a method which takes a @FunctionalInterface as a parameter. I can see that passing a lambda like
Copy code
.filter{req: ClientRequest, next: ExchangeFunction ->
    // do something
}
works. However, what I'd like to do is pass a function reference instead of the lambda so that I don't have to actually put 50 lines of code inside the
.filter
call. I'm quite new to Kotlin and I have a very fragile understanding of higher order functions, so I have no idea how to achieve this. I thought making the method like this -
Copy code
private fun customFilter(request: ClientRequest, next: ExchangeFunction){

}
and calling like
.filter(::customFilter)
would work, but it didn't - IDE tells me
Copy code
Type mismatch.
Required:
ExchangeFilterFunction
Found:
KFunction2<@ParameterName ClientRequest, @ParameterName ExchangeFunction, Unit>
It seems like I have to somehow implement the ExchangeFilterFunction.java which is a java
@FunctionalInterface
...? Any advice is appreciated
g
The customFilter method is defined on the ClientRequest class (https://kotlinlang.org/docs/tutorials/kotlin-for-py/member-references-and-reflection.html), so the method reference is bound to an instance of that class. I think this should work
Copy code
val x = ClientRequest(...)
.filter(x::customFilter)
if there is no need for customFilter to access the instance, put it in the companion object, then you should be able to do ClientRequest::customFilter. I don't have time to try it out though, so I'm not a 100% sure about this