我即将把一些java代码移植到kotlin。我刚刚发现了一些笨拙的工厂方法,它看起来非常简单:
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.appender.SyslogAppender;
public class Test {
public Appender getSyslogAppender(String id, String host, int port, boolean immediateFlush, boolean newLine) {
return SyslogAppender.newSyslogAppenderBuilder()
.setId(id)
.setNewLine(newLine)
.withHost(host)
.withPort(port)
.withImmediateFlush(immediateFlush)
.build();
}
}
所以我的第一次尝试是:
fun getSyslogAppender(id: String, host: String, port: Int, immediateFlush: Boolean, newLine: Boolean): Appender {
return SyslogAppender.newSyslogAppenderBuilder()
.setId(id)
.setNewLine(newLine)
.withHost(host)
.withPort(port)
.withImmediateFlush(immediateFlush)
.build()
}
但是kotlin编译器并不开心。似乎不确定应该返回什么类型:
Type inference failed: Not enough information to infer parameter B in fun <b : syslogappender.builder!> newSyslogAppenderBuilder(): B! Please specify it explicitly.
好的,我们给它一个类型:
SyslogAppender.newSyslogAppenderBuilder<SyslogAppender.Builder>()
但等等:
One type argument expected for class Builder<b : syslogappender.builder!> : Builder, SocketAppender.AbstractBuilder defined in org.apache.logging.log4j.core.appender.SyslogAppender
让我们看看定义:
public class SyslogAppender extends SocketAppender {
public static class Builder<B extends Builder<B>> extends AbstractBuilder<B>
implements org.apache.logging.log4j.core.util.Builder<SocketAppender>
什么???类型为B的构建器。其中B具有B的超类型构建器吗? ......我们以后的一些思考和实验......我不明白。
有人知道如何解决这个问题吗?我确信这很简单。