Rodney Nogueira
Rodney Nogueira

Reputation: 685

Java Lambda to Kotlin

I'm building my code with Kotlin.

I've stumbled upon a problem using Lambda in Kotlin with the following:

Java code:

  ((UndertowEmbeddedServletContainerFactory) container)
        .addBuilderCustomizers(builder ->
        builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true));

Using common interface instantiation

((UndertowEmbeddedServletContainerFactory) container)
            .addBuilderCustomizers(new UndertowBuilderCustomizer() {
                @Override
                public void customize(Builder builder) {
                    builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true);
                }
            });

My code in Kotlin

val c: UndertowEmbeddedServletContainerFactory = (container as UndertowEmbeddedServletContainerFactory)
// Calling the Lambda
c.addBuilderCustomizers{ (b: Builder) -> b.setServerOption(UndertowOptions.ENABLE_HTTP2, true) }

It's giving me a syntax error:

Multiple markers at this line - Passing value as a vararg is only allowed inside a parenthesized argument list - Cannot infer a type for this parameter. Please specify it explicitly.

What might be the correct syntax to this?

Upvotes: 2

Views: 1500

Answers (1)

Rafal G.
Rafal G.

Reputation: 4432

You need to help Kotlin compiler a bit and tell it what is the type of this lambda. This code should compile and work just fine:

        c.addBuilderCustomizers(UndertowBuilderCustomizer{ it.setServerOption(UndertowOptions.ENABLE_HTTP2, true)})

Upvotes: 4

Related Questions