Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need More Clarity in "Function Type" Reference #324

Open
Alc-Alc opened this issue Jul 22, 2024 · 1 comment
Open

Need More Clarity in "Function Type" Reference #324

Alc-Alc opened this issue Jul 22, 2024 · 1 comment

Comments

@Alc-Alc
Copy link

Alc-Alc commented Jul 22, 2024

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 Alc-Alc changed the title Needs More Clarity in "Function Type" Docs Need More Clarity in "Function Type" Reference Jul 22, 2024
@Alc-Alc
Copy link
Author

Alc-Alc commented Jul 23, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant