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

preference of initializer: .init() vs MyType( #1903

Open
jonnyijapan opened this issue Oct 15, 2024 · 1 comment
Open

preference of initializer: .init() vs MyType( #1903

jonnyijapan opened this issue Oct 15, 2024 · 1 comment

Comments

@jonnyijapan
Copy link

jonnyijapan commented Oct 15, 2024

Let's say we have a type, like a struct MyType.

Is there a rule such that we can force either .init() style or MyType() style initialization?
If not, would it be possible to create such a rule / formatter?

This looks slightly similar but is probably different https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#redundantinit because it assumes that the type is always written out. This issue is for the cases where it's only written like .init(, thus letting the compiler infer the type.

@nicklockwood
Copy link
Owner

It's not possible to solve in the general case because SwiftFormat doesn't have access to non-local type information, so if you've written someFunction(.init()) I don't know the type of the argument and can't replace it with the type, and if you've written someFunction(Foo()), I don't know if the argument type was Foo or Any or some superclass or protocol that Foo conforms to, in which case replacing it with .init() wouldn't be valid.

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

No branches or pull requests

2 participants