Kotlin: ‘public’属性公开其’local’类型参数<未提供名称>

huangapple go评论101阅读模式
英文:

Kotlin: 'public' property exposes its 'local' type argument <no name provided>

问题

interface A {
    fun f(): String
}

val B = { attr: String ->
    object : A {
        override fun f() = attr
    }
}

在定义 B 处我遇到了以下错误:

'public' 属性暴露了其 'local' 类型参数 <no name provided>

这个错误的原因是什么?

英文:
interface A {
    fun f() : String
}

val B = { attr : String -&gt;
    object : A {
        override fun f() = attr
    }
}

I'm getting this error at the definition of B:

&#39;public&#39; property exposes its &#39;local&#39; type argument &lt;no name provided&gt;

What is the cause of this error?

答案1

得分: 2

你正在返回一个匿名的单例对象,而Kotlin无法确定未提供名称的类型。您可以明确指定 B 的类型以解决此问题。

val B: (String) -> A = { attr: String ->
    object : A {
        override fun f() = attr
    }
}
英文:

You are returning an anonymous singleton object, and kotlin is not able to figure out the type for which no name is provided. You can specify the type of B explicitly to fix this issue

val B: (String) -&gt; A = { attr: String -&gt;
    object : A {
        override fun f() = attr
    }
}

huangapple
  • 本文由 发表于 2020年9月7日 12:42:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63771490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定