Reputation: 889
I'm struggling with understanding return values in Swift. Can you explain the difference between these?
func someFunc() -> Void {}
func someFunc() {}
Upvotes: 16
Views: 17605
Reputation: 127
It's just some internal Swift inconsistency. Both definitions define the same, but:
func a() -> () {}
func b() {} //by default has same signature as a
func c() -> Void {} //has different signature than a && b
so:
var x:(() -> Void) = c //OK
var y:(() -> ()) = a //OK
var z:(() -> ()) = c //ERROR - different signatures even if they mean the same
x = a //ERROR - different signatures, can't convert "() -> Void" into "() -> ()"
Upvotes: 4
Reputation: 52213
Simply, there is no difference. -> Void
is just an explicit way of saying that the function returns no value.
From docs:
Functions without a defined return type return a special value of type Void. This is simply an empty tuple, in effect a tuple with zero elements, which can be written as ().
Thus, these three function declarations below are same:
func someFunc() {}
func someFunc() -> Void {}
func someFunc() -> () {}
Upvotes: 26