swift-book icon indicating copy to clipboard operation
swift-book copied to clipboard

Need More Clarity in "Function Type" Reference

Open Alc-Alc opened this issue 7 months ago • 1 comments

Location

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/types#Function-Type

Description

the function type (Void) -> Void is the same as (()) -> ()

If you use the former ((Void) -> Void) as a typealias, Swift warns. I am assuming things changed on a newer version but the docs were not updated?

// warning: when calling this function in Swift 4 or later, you must pass a '()' tuple; did you mean for the input type to be '()'?
typealias warningAlias = (Void) -> Void

These types aren’t the same as () -> () — a function that takes no arguments.

But in the snippet that follows, all are true, meaning the types are same.

typealias aliasOne = ((Void)) -> Void
typealias aliasTwo = () -> ()
typealias aliasThree =  (()) -> ()
typealias warningAlias = (Void) -> Void

// all are true
print(aliasOne.self == aliasTwo.self)
print(aliasTwo.self == aliasThree.self)
print(aliasOne.self == aliasThree.self)

// all are still true, comparing with an alias that emits a warning
print(aliasOne.self == warningAlias.self)
print(aliasTwo.self == warningAlias.self)
print(aliasThree.self == warningAlias.self)

PS: All the quotes are taken from the aforementioned link and code snippets were run on Swift 5.10.

Correction

There is a chance I am just misunderstanding things, but I feel this part could be improved by not referring to a type ((Void) -> Void) that emits a warning and preferably explaining more on how they differ as the code snippet I have shown tells me (again, it could very well be a misunderstanding 🙂) that they do not differ.

Edit: I have asked over at https://forums.swift.org/t/understanding-function-types/73349. I will close the issue if there is something wrong in my understanding, thanks and apologies 🙂

Alc-Alc avatar Jul 22 '24 03:07 Alc-Alc