-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Rule Request: Prefer Self
over type(of: self)
#3003
Comments
Why opt-in? Are there cases where this isn't preferred or where the semantics are different? |
Ah I know now, it's for support for Swift < 5.1 |
Also I’m not sure if the community has settled on one or another |
Is there a performance benefit to EDIT: Spent the last few minutes playing around with some test cases, and looking at optimized sil. Looks like the compiler is smart enough to optimize away the type(of:) when it can. Also interestingly there is different sil for Self vs type(of:). Not sure what the difference is. Test File:
Emit sil command: The interesting cases: Self:
type(of: self)
For the most part it's the same, I see 2 differences.
type(of: self) uses:
Note: sil probably doesn't tell the whole story. TLDR: It's basically the same. Trust the compiler to make the right optimizations. (It is interesting that they are optimized differently. If one is better than the other, then this may be an area for improvement). |
Thanks for the thoughtful analysis, @cltnschlosser ! ❤️ |
New Issue Checklist
#2944 and #2921 are related, but I think this should be a separate rule.
New rule request
the community thinks about this.
Swift 5.1 introduces the
Self
type, which can be used in place oftype(of: self)
.Just severity.
See README.md for guidelines on when to mark a rule as opt-in.
Opt-in.
The text was updated successfully, but these errors were encountered: