【发布时间】:2019-05-22 12:16:04
【问题描述】:
我正在使用swift-snapshot-testing,发现一个关于泛型函数参数默认值的问题。
框架提供了这样一个方法:
func verifySnapshot<Value, Format>(matching value: Value,
as snapshotting: Snapshotting<Value, Format>,
snapshotDirectory: String? = nil) -> String?
其中Snapshotting 是这样的通用结构:
struct Snapshotting<Value, Format> {}
extension Snapshotting where Value == UIViewController, Format == UIImage {
static var image: Snapshotting<UIViewController, UIImage> {
:
}
}
extension Snapshotting where Value == UIView, Format == UIImage {
static var image: Snapshotting<UIView, UIImage> {
:
}
}
我想创建一个辅助方法,这很有效:
func verify<Value, Format>(matching value: Value,
as snapshotting: Snapshotting<Value, Format>) {
let snapshotDirectory = "/path"
let failure = verifySnapshot(matching: value,
as: snapshotting,
snapshotDirectory: snapshotDirectory)
print(failure ?? "Done!")
}
但是当我想给snapshotting一个默认参数值.image时,它不会编译错误Ambiguous reference to member 'image'
func verify<Value, Format>(matching value: Value,
as snapshotting: Snapshotting<Value, Format> = Snapshotting<Value, Format>.image)
我的问题是:Swift 可以推断泛型类型Format 的默认参数值吗?
【问题讨论】: