Search code examples
lambdakotlindisruptor-pattern

LMAX Disruptor with Kotlin: Can't use lambda?


For example in Java I can do this with LMAX disruptor:

Disruptor<NetworkEvent> disruptor = new Disruptor<>(NetworkEvent::new, 2048, Executors.newSingleThreadedExecutor());

In Kotlin I try this equivalent:

val disruptor = Disruptor<NetworkEvent>({ NetworkEvent() }, 2048, Executors.newSingleThreadExecutor())

But I'm greeted with this error:

enter image description here


Solution

  • First of all, you dont need to specify T, kotlin can infer it.

    Second the error message says whats wrong, Disruptor wants a EventFactory, but you pass it a simple lambda.

    If you want to pass a lambda as SAM Interface, sometimes its needed to preceed the lambda with the interface name.

    Try the following (untested):

    val disruptor = Disruptor(EventFactory { NetworkEvent()}, 2048,....)