Benoît Liessens
05/25/2023, 6:31 AMpackage org.apache.camel;
public interface Predicate {
boolean matches(Exchange exchange);
}
public interface Expression {
<T> T evaluate(Exchange exchange, Class<T> type);
}
Kotlin accepts following property declaration
val somePredicate = Predicate { exchange -> true}
For the Expression
interface however, Koltin does not accept a similar construct. I must explicitly declare an object and implement the interface method like this:
val someExpression = object : Expression {
override fun <T : Any?> evaluate(exchange: Exchange?, type: Class<T>?): T {
TODO("Not yet implemented")
}
}
I fail to understand why however?ephemient
05/25/2023, 6:33 AM<T>
Adam S
05/25/2023, 6:47 AMExpression
to Kotlin and try making it a functional interface you’ll get a more specific error message
Single abstract member cannot declare type parameters
Benoît Liessens
05/25/2023, 7:12 AMBenoît Liessens
05/25/2023, 7:14 AMPredcate { .. }
construct have a specific name ?Benoît Liessens
05/25/2023, 7:14 AMval somePredicate : Predicate = { exchange -> true}
Adam S
05/25/2023, 7:17 AMAdam S
05/25/2023, 7:19 AMval somePredicate: Predicate = { exchange -> true}
on the left there’s Predicate
, but the value has a different type
val blahPredicate: (Exchange) -> Boolean = { exchange: Exchange -> true}
Predicate
!= (Exchange) -> Boolean
Benoît Liessens
05/25/2023, 7:22 AM@FunctionalInterface
annotation on Predicate
change anything wrt Predicate
!= (Exchange) -> Boolean
?Adam S
05/25/2023, 7:28 AMPredicate {}
can be skipped if it’s a function parameter https://kotlinlang.org/docs/java-interop.html#sam-conversions
What you could do is write your own implementation of Predicate
using a typealias
typealias Predicate2 = (Exchange) -> Boolean
And then write extension functions for all Apache Camel methods that accept a Predicate
to convert from Predicate2
to Predicate
, but that sounds like a lot of work for not much gain…Benoît Liessens
05/25/2023, 7:34 AMephemient
05/25/2023, 8:27 AMfun someKotlinFunction(predicate: (Exchange) -> Boolean) {
someJavaCamelFunction(..., { predicate(it) }, ...)
or a converter (doesn't save all that much here, but may be more convenient when there are more parameters)
inline fun Predicate(crossinline predicate: (Exchange) -> Boolean): Predicate = Predicate { predicate(it) }
fun someKotlinFunction(predicate: (Exchange) -> Boolean) {
someJavaCamelFunction(Predicate(predicate))
than to wrap every function that takes a Predicate
ephemient
05/25/2023, 8:33 AMPredicate(predicate)
to convert a (Exchange) -> Boolean
to a Predicate
ephemient
05/25/2023, 8:33 AMAdam S
05/25/2023, 8:35 AM