有时使用泛型时,我会收到一条引用" _"的错误消息。作为参数。它似乎没有记录。这是什么意思?
举个例子,我收到错误:
protocol SuperheroType {
typealias Superpower
}
struct JawDroppingFeat<Superhero: SuperheroType where Superhero: Arbitrary, Superhero.Superpower: Arbitrary>: Arbitrary {
let subject: Superhero
let superpowerUsed: Superhero.Superpower
static var arbitrary: Gen<JawDroppingFeat<Superhero>> {
get {
return Gen.zip(Superhero.arbitrary, Superhero.Superpower.arbitrary)
.map{ (x: Superhero, y: Superhero.Superpower) in
JawDroppingFeat(subject: x, superpowerUsed: y)
}
}
}
}
当我尝试编译时:
public struct Gen<A> {
public static func zip<A, B>(gen1: SwiftCheck.Gen<A>, _ gen2: SwiftCheck.Gen<B>) -> SwiftCheck.Gen<(A, B)>
public func map<B>(f: A -> B) -> SwiftCheck.Gen<B>
}
public protocol Arbitrary {
public static var arbitrary: SwiftCheck.Gen<Self> { get }
}
Gen和Arbitrary类型来自SwiftCheck,相关声明是:
<_>
我认为_
与swift无法推断类型参数有关,而不是Chris Lattner对我畏缩的图像。但它是否有更精确(和记录)的含义?
我当前最好的理论是,当Swift无法推断出一个类型参数,而不是立即失败时,它会分配一个null(.map
)类型,这会导致实际编译错误在某个不兼容的地方下游<在我的例子中,传递给{
"name": "shreeji/ring",
"description": "Simple",
"license": "MIT",
"authors": [
{
"name": "author",
"email": "email@gmail.com"
}
],
"autoload": {
"psr-4": {
"Shreeji\\Ring\\": "src/"
}
},
"minimum-stability": "dev",
"require": {
"Illuminate/support": "~5"
}
}
的参数)。
答案 0 :(得分:1)
这意味着您在参数的返回值中包含不完整的类型信息。
在这种情况下,.map函数返回一个没有指定嵌入类型的通用JawDroppingFeat。
我认为你打算写
JawDroppingFeat<SuperHero>(subject: x, superpowerUsed: y)
答案 1 :(得分:1)
一般来说,我的经验是此错误消息似乎是向后构建的。 “无法将X类型的值转换为Y”似乎意味着“您提供了Y,但需要的是X.”