简体   繁体   English

使用 IntelliJ 将 Java lambda 转换为 Kotlin Lamba 生成语法错误

[英]Transform Java lambda to Kotlin Lamba with IntelliJ generate syntax error

I have a Java class that configure the embebed tomcat of Spring Boot 2.0.6 for use HTTP2 protocol:我有一个 Java 类,它配置 Spring Boot 2.0.6 的嵌入 tomcat 以使用 HTTP2 协议:

package com.talleres.paco.mako.config;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http2.Http2Protocol;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TomcatHttp2Config {
    @Bean
    public ConfigurableServletWebServerFactory tomcatCustomizer() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers((Connector connector) -> {
            connector.addUpgradeProtocol(new Http2Protocol());
        });
        return factory;
    }
}

When I use IntelliJ for convert this snippet code to Kotlin I obtain a compilation error.当我使用 IntelliJ 将此片段代码转换为 Kotlin 时,出现编译错误。 Here is the Kotlin code that IntelliJ return:这是 IntelliJ 返回的 Kotlin 代码:

package com.talleres.paco.mako.config

import org.apache.catalina.connector.Connector
import org.apache.coyote.http2.Http2Protocol
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class TomcatHttp2Config {
    @Bean
    fun tomcatCustomizer(): ConfigurableServletWebServerFactory {
        val factory = TomcatServletWebServerFactory()
        factory.addConnectorCustomizers({ connector: Connector -> connector.addUpgradeProtocol(Http2Protocol()) })
        return factory
    }
}

I obtain this error:我得到这个错误:

Type mismatch.
Required:
TomcatConnectorCustomizer!
Found:
(Connector) → Unit

At line that appears the lambda:在出现 lambda 的行:

    factory.addConnectorCustomizers({ connector: Connector -> connector.addUpgradeProtocol(Http2Protocol()) })

How I can translate the Java code above to Kotlin.我如何将上面的 Java 代码翻译成 Kotlin。 Thanks in advance.提前致谢。

You could look into SAM conversions for Kotlin .您可以查看Kotlin 的SAM 转换

If for some reason the version with the method doesn't work如果由于某种原因,该方法的版本不起作用

factory.addConnectorCustomizers { it.addUpgradeProtocol(Http2Protocol()) }

You can try to implement the Java interface easily as您可以尝试轻松实现Java接口

factory.addConnectorCustomizers(TomcatConnectorCustomizer { it.addUpgradeProtocol(Http2Protocol()) })

It's a probable Kotlin compiler bug that manifests itself when the functional argument is a vararg.这是一个可能的 Kotlin 编译器错误,当函数参数是 vararg 时就会表现出来。 Here's a minimal reproducer:这是一个最小的复制器:

Java:爪哇:

public class Runner {
    void addTasks(Runnable... tasks) { }
}

Kotlin:科特林:

fun main(args: Array<String>) {
    Runner().addTasks({ println("success") })
}

Error:(4, 26) Kotlin: Type mismatch: inferred type is () -> Unit but Runnable!错误:(4, 26) Kotlin:类型不匹配:推断类型为 () -> 单元但可运行! was expected预料之中

If we change java:如果我们改变java:

public class Runner {
    void addTask(Runnable task) { }
}

the error goes away.错误消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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