Reputation: 8158
This is a totally contrived example I wrote while learning about function overloading in Swift. The following functions differ only in return type (the first function implicitly returns Void
/ ()
while the other returns 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
Is there a way I can call the Void
version without using let
, i.e. some type of cast? Again, I realize this is a contrived example but I'd like to understand the options here.
Upvotes: 4
Views: 318
Reputation: 154583
You can disambiguate the two foo
functions by casting the result:
foo(6, y: 7) as Int
foo(6, y: 7) as Void
or you can cast foo
itself:
(foo as (Int, y: Int) -> Int)(6, y: 7)
(foo as (Int, y: Int) -> Void)(6, y: 7)
Note: ()
may be used in place of Void
in both instances.
Upvotes: 6