Comments (10)
Ah, thanks for that clarification Danny. If I replace override class var
with override static var
then everything compiles and SwiftLint does not show a violation warning. I believe I have a better understanding now of what the rule tries to accomplish.
Hopefully this GitHub issue will help others better understand as well!
from swiftlint.
TIL:
static
is an alias forfinal class
: https://stackoverflow.com/a/29206635/255489
This is one aspect of the rule. They are both equivalent, so the rule suggests to prefer the shorter one. In final classes, final
is implicitly applied to all contained declarations, thus every class
becomes final class
.
Is there any functional difference between a class method and a static method on a final class?
I don't think there is, at least not in Swift. Perhaps there are both class
and static
in Swift because of backwards compatibility with Objective-C. But this is not my competence.
Anyway, due to having two keywords, the prescribed meaning for class
is "can be overridden" as opposed to static
which cannot. However, together with final
, nothing can actually be overridden, hence the suggestion to use static
instead.
Incidentally, the Swift compiler complains about open
being used in final
classes or declarations likewise.
My suggestions are "Prefer static
over final class
" on f
in
class C1 {
final class func f() {}
}
and "Prefer static
over class
in a final class" on f
in
final class C2 {
class func f() {}
}
Does that make things clearer?
from swiftlint.
I'm seeing the same in my project, in other NSObject-derived classes.
from swiftlint.
The intention of the "non-overridable" in the documentation is like "elements that cannot be overridden further should be marked static
instead of final class
".
In your example, static
instead of class
should be valid. Or are you seeing any compilation issues after the change?
from swiftlint.
Perhaps it's better to remove the part "for non-overridable declarations" from the message entirely. Anyone willing to do that? 😄
from swiftlint.
If I did, I would explicitly add this case to the static over final documentation for both triggering and non-triggering examples since it calls out Xcode boilerplate code that's bound to trigger a violation upon project creation.
However, my job prevents me from contributing to open source software so hopefully someone else can take the lead here 😅
from swiftlint.
Yeah, the wording of the warning here isn't clear. I took "non-overridable declarations" to mean methods that weren't overridden.
from swiftlint.
I still find the wording to be confusing:
SomeFile.swift:8:5: error: Static Over Final Class Violation: Prefer 'static' over 'final class' (static_over_final_class)
For this call site:
final class SomeClass: NSObject {
@objc class func someFunc(...)
The confusing part is that the error message says "over final class
", but it's not the final class
part we're changing. We're changing class func
to static func
.
from swiftlint.
"Prefer static methods over class methods on final classes"?
For interest: I'm honestly unclear what the benefit is to this rule — is it just for consistency? (It would seem not, given that it's only catching final classes).
Is there any functional difference between a class method and a static method on a final class?
from swiftlint.
TIL: static
is an alias for final class
: https://stackoverflow.com/a/29206635/255489
This could be part of the confusion: I didn't know final class
functions were a thing in Swift, so seeing that in the error message was confusing. Maybe leave it out if it was like the declaration I mentioned?
from swiftlint.
Related Issues (20)
- The baseline use absolute path instead of relative file in the baseline content to identify a file HOT 2
- [0.55.1] False positive on `unused_element` where element and index are used separately in subsequent blocks HOT 2
- `opening_brace` now triggers on multiline if statements HOT 1
- SwiftLintCommandPlugin Unknown option '--target' HOT 2
- Ignore .build folders by default HOT 5
- `baseline compare` produces some false alarms
- SwiftLint configuration ignored during Xcode build? HOT 4
- Autocorrect for `redundant_void_return` can cause code breakage HOT 2
- Error running in `node:slim-21` docker image
- void_function_in_ternary incorrectly triggered with if and switch expressions HOT 1
- change excludingSyntaxKinds for VerticalWhitespaceBetweenCasesRule.swift HOT 4
- Rule Request: No Empty Function HOT 2
- Upload Privacy Policy HOT 1
- Enforce attributes to be on the same line only if they don't have arguments HOT 1
- Xcode 15 'Internal inconsistency error' when using as a Build Tool Plug-In HOT 1
- False positive of extension_access_modifier on property with private(set)
- Incorrect setting name for package plugin validation in README.md
- SwiftLint is recognizing Typed-Error as identifier HOT 2
- skipping cache due to an error: https://github.com/realm/SwiftLint: The repository could not be found. Make sure a valid repository exists at the specified location and try again. HOT 1
- missing_docs doesn't trigger for non-inherited properties/functions when using excludes_inherited_types HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftlint.