这是我在学习Swift函数重载时编写的完全虚构的示例。以下函数仅在返回类型上有所不同(第一个函数隐式返回Void
/ ()
,而另一个函数返回Int
)。
func foo(x:Int, y:Int) {
}
func foo(x:Int, y:Int) -> Int {
return 0
}
// Call the version which returns Int
let i: Int = foo(6, y: 7)
// Call the version which returns Void
let v: Void = foo(6, y: 7)
// Ambiguous
foo(6, y:7) // How can I force a call to the Void version without using let/var?
// I thought this might work but doesn't
foo(6,y: 7) as (Int, Int) -> Void
有没有一种方法可以在不使用let
的情况下调用Void
版本,即某种类型的转换?再次强调,我意识到这是一个人为的示例,但我想了解这里的选项。