2

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?

1
  • Try wrapping the lambda in parentheses. Commented Aug 23, 2017 at 18:05

1 Answer 1

4

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)})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.