Given these 2 Apache Camel (Java) interfaces ```pa...
# getting-started
b
Given these 2 Apache Camel (Java) interfaces
Copy code
package 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
Copy code
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:
Copy code
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?
e
the lambda form can't be parameterized over all
<T>
a
if you convert
Expression
to Kotlin and try making it a functional interface you’ll get a more specific error message
Single abstract member cannot declare type parameters
b
Thanks for that @ephemient and @Adam S 👍
Continuing on the example with `Predicate`: does that
Predcate { .. }
construct have a specific name ?
And why is this not accepted?
Copy code
val somePredicate  : Predicate = { exchange -> true}
a
Hmm I’m not sure if there’s a specific name given https://kotlinlang.org/docs/fun-interfaces.html#sam-conversions
Copy code
val somePredicate: Predicate = { exchange -> true}
on the left there’s
Predicate
, but the value has a different type
Copy code
val blahPredicate: (Exchange) -> Boolean = { exchange: Exchange -> true}
Predicate
!=
(Exchange) -> Boolean
b
Would a
@FunctionalInterface
annotation on
Predicate
change anything wrt
Predicate
!=
(Exchange) -> Boolean
?
a
I think not, but give it a go. Explicitly typing
Predicate {}
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
Copy code
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…
b
Thanks @Adam S
👍 1
e
it would probably be less work to write
Copy code
fun 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)
Copy code
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
although now that I check, that's not actually necessary; even without a constructor-like wrapper, you can write
Predicate(predicate)
to convert a
(Exchange) -> Boolean
to a
Predicate
hmm. was that always possible and I never noticed?
a
@ephemient apparently there was a change in 1.6.20? https://kotlinlang.org/docs/fun-interfaces.html#migration-from-an-interface-with-constructor-function-to-a-functional-interface not sure if it’s related but it looks similar?