【问题标题】:"Not enough information to infer type variable B" when calling ConfigurationBuilder.newComponent()调用 ConfigurationBuilder.newComponent() 时“没有足够的信息来推断类型变量 B”
【发布时间】:2021-02-19 15:30:00
【问题描述】:

我正在尝试在我的应用程序中实现 log4j2 的编程配置,但是当我调用 ConfigurationBuilder.newComponent() 方法时,我得到了 "Not enough information to infer type variable B" 编译器错误。

这里是sn-p的代码:

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 的新手,不确定在这种情况下如何显式指定方法的返回类型。

【问题讨论】:

    标签: kotlin log4j2


    【解决方案1】:

    您可以从addComponent() 的签名中看到它需要Component&lt;?&gt;,因此B 的类型无关紧要。在 Kotlin 中,当不需要或未知类型时,您可以使用 * 代替 Java 中的 ?。所以你可以使用ComponentBuilder&lt;*&gt;作为newComponent的返回类型。

    .addComponent(
        builder.newComponent<ComponentBuilder<*>>("Field")
            .addAttribute("literal", "some value")
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 1970-01-01
      • 2021-02-23
      • 2020-03-11
      • 1970-01-01
      • 2022-11-30
      相关资源
      最近更新 更多