Axon with Kotlin: method "apply" not acessible inside @CommandHandler mehtod

205 views
Skip to first unread message

Fabrício Rissetto

unread,
May 27, 2018, 11:06:07 PM5/27/18
to Axon Framework Users
Hello people,

I am trying to use AxonFramework v3.2.2 inside my Koltin project. But Kotlin is recognizing the method `apply` as the kotlin inline function (from its stdlib), instead of the method `apply` located inside the AggregateLyfeCycle class (what I think would be the right one). 

import org.axonframework.commandhandling.CommandHandler
import org.axonframework.commandhandling.model.AggregateIdentifier
import org.axonframework.eventsourcing.EventSourcingHandler
import org.axonframework.spring.stereotype.Aggregate
//other imports here ...

@Aggregate
class Order {
@AggregateIdentifier
var id: Int = 0

lateinit var user: User
val products = mutableListOf<Product>()

@CommandHandler
fun createOrder(command: CreateOrderCommand) {
apply(OrderCreatedEvent(orderId = 1, user = command.user)) //Doesn't compile!
}

@EventSourcingHandler
fun on(event: OrderCreatedEvent) {
this.id = event.orderId
}
}

The error is:
Error:(22, 9) Kotlin: Type inference failed: inline fun <T> T.apply(block: T.() -> Unit): T
cannot be applied to
receiver: Order  arguments: (OrderCreatedEvent)

Do you know what could be the problem? Any help would be appreciated.
 

Fabrício Rissetto

unread,
May 27, 2018, 11:20:43 PM5/27/18
to Axon Framework Users
Got it. 

The apply method is a static method, so it is needed to import it (or all it explicitly). 

Java way:
import static org.axonframework.commandhandling.model.AggregateLifecycle.apply;

Kotlin way:
import org.axonframework.commandhandling.model.AggregateLifecycle.apply

Steven van Beelen

unread,
May 30, 2018, 4:22:36 AM5/30/18
to axonfr...@googlegroups.com
Great you figured it out Fabricio and thanks for sharing that info with everybody!

--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages