swiftlang / swift-book

The Swift Programming Language book
Apache License 2.0
1.72k stars 159 forks source link

Need More Clarity in "Function Type" Reference #324

Open Alc-Alc opened 1 month ago

Alc-Alc commented 1 month ago

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 commented 1 month ago

So this was an issue with my understanding, the only "issue" I would like to point out is this.

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?

You can close the issue if you want. Thanks again.