MeanwhileInHell
MeanwhileInHell

Reputation: 7053

Kotlin : Overload resolution ambiguity

New to Kotlin. I am using Apache Camel and have created a route using the process transformer like so:

from("snmp:blahblah...")
    .routeId("CamelSnmpRoute")
    ...
    .process {                          <<< Here
        logger.debug("Log stuff")
    }

Error:

Overload resolution ambiguity. All these functions match.
* public final fun process(processor: (() -> Processor!)!): RouteDefinition! defined in org.apache.camel.model.RouteDefinition
* public final fun process(processor: ((exchange: Exchange!) -> Unit)!): RouteDefinition! defined in org.apache.camel.model.RouteDefinition

I have tried doing .process { () -> but it doesn't like that, saying it is expecting a name between the brackets. In the mean time, I can get past the erroring using .process { exchange -> and not using the exchange var, or creating a logProcessor var and passing it in:

    .process(logProcessor)
}

private var logProcessor: Processor = Processor {
    logger.debug("Logging stuff")
}

Can someone tell me how to inline this var so as to not create the ambiguity, or a redundant var?

Upvotes: 0

Views: 1705

Answers (1)

Tenfour04
Tenfour04

Reputation: 93521

Looking at the docs, I can't find the first of those two ambiguous functions. Assuming it comes from SAM conversion, it looks like in Java it would be method that takes some sort of ProcessorFactory interface, in which case it almost certainly would be then internally calling the factory method and passing the result to the second ambiguous method.

So either way, the exchange variable is going to exist at some point. Omitting it would only change its name to the default it.

If you aren't going to use a function parameter, you can use an underscore for its name to improve readability:

from("snmp:blahblah...")
    .routeId("CamelSnmpRoute")
    ...
    .process { _ -> logger.debug("Log stuff") }

You can alternately express the specific interface type like this:

from("snmp:blahblah...")
    .routeId("CamelSnmpRoute")
    ...
    .process (Processor{ logger.debug("Log stuff") })

Upvotes: 3

Related Questions