简体   繁体   中英

Implementing Akka AbstractActor in Groovy produces compiler errors

Groovy 2.4.7 here using the Akka Java API with Gradle coordinates: com.typesafe.akka:akka-actor_2.11:2.5.4 .

I'm trying to implement an AbstractActor and running into a potential conflict between the Java/Akka API and Groovy itself:

class MyActor extends AbstractActor {
    @Override
    Receive createReceive() {
        receiveBuilder()
            .match(DoSomething, message -> {
            // Implement message handler if message is of type DoSomething
            }).build()
    }
}

Produces the following compiler error:

" Groovy:expecting EOF, found ')' @ line 18, column 5. "

I think it has something to do with the -> operator used after message. Is there a fix or workaround for this in Groovy-land?

You've mixed up java's lambda expressions with groovy's closures. So it should be:

class MyActor extends AbstractActor {
    @Override
    Receive createReceive() {
        receiveBuilder()
            .match(DoSomething, { message ->
            // Implement message handler if message is of type DoSomething
            }).build()
    }
}

Please have a look here as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM