"Not enough information to infer type variable B" 调用 ConfigurationBuilder.newComponent() 时
"Not enough information to infer type variable B" when calling ConfigurationBuilder.newComponent()
我试图在我的应用程序中实现 log4j2 的编程配置,但是当我调用 ConfigurationBuilder.newComponent()
方法时,出现 "Not enough information to infer type variable B"
编译器错误。
这是代码片段:
val builder = ConfigurationBuilderFactory.newConfigurationBuilder()
val graylogAppender = builder.newAppender("Graylog", "Gelf")
.addAttribute("host", "tcp:localhost")
.addAttribute("port", 12201)
.addComponent(
builder.newComponent("Field") // <-- Error here
.addAttribute("literal", "some value")
)
Java 中的相同代码编译得很好。
newComponent()
的签名是:
<B extends ComponentBuilder<B>> ComponentBuilder<B> newComponent(String pluginName);
我是 Kotlin 的新手,不太确定在这种情况下如何显式指定方法的 return 类型。
您可以从 addComponent()
的签名中看出它需要 Component<?>
,因此类型 B
无关紧要。在 Kotlin 中,当类型不需要或未知时,您可以使用 *
而不是 ?
,就像在 Java 中那样。因此,您可以将 ComponentBuilder<*>
用作 newComponent
.
中的 return 类型
.addComponent(
builder.newComponent<ComponentBuilder<*>>("Field")
.addAttribute("literal", "some value")
)
我试图在我的应用程序中实现 log4j2 的编程配置,但是当我调用 ConfigurationBuilder.newComponent()
方法时,出现 "Not enough information to infer type variable B"
编译器错误。
这是代码片段:
val builder = ConfigurationBuilderFactory.newConfigurationBuilder()
val graylogAppender = builder.newAppender("Graylog", "Gelf")
.addAttribute("host", "tcp:localhost")
.addAttribute("port", 12201)
.addComponent(
builder.newComponent("Field") // <-- Error here
.addAttribute("literal", "some value")
)
Java 中的相同代码编译得很好。
newComponent()
的签名是:
<B extends ComponentBuilder<B>> ComponentBuilder<B> newComponent(String pluginName);
我是 Kotlin 的新手,不太确定在这种情况下如何显式指定方法的 return 类型。
您可以从 addComponent()
的签名中看出它需要 Component<?>
,因此类型 B
无关紧要。在 Kotlin 中,当类型不需要或未知时,您可以使用 *
而不是 ?
,就像在 Java 中那样。因此,您可以将 ComponentBuilder<*>
用作 newComponent
.
.addComponent(
builder.newComponent<ComponentBuilder<*>>("Field")
.addAttribute("literal", "some value")
)