Giter VIP home page Giter VIP logo

Comments (10)

amcinnis avatar amcinnis commented on September 26, 2024 1

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.

SimplyDanny avatar SimplyDanny commented on September 26, 2024 1

TIL: static is an alias for final 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.

tonyarnold avatar tonyarnold commented on September 26, 2024

I'm seeing the same in my project, in other NSObject-derived classes.

from swiftlint.

SimplyDanny avatar SimplyDanny commented on September 26, 2024

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.

SimplyDanny avatar SimplyDanny commented on September 26, 2024

Perhaps it's better to remove the part "for non-overridable declarations" from the message entirely. Anyone willing to do that? 😄

from swiftlint.

amcinnis avatar amcinnis commented on September 26, 2024

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.

tonyarnold avatar tonyarnold commented on September 26, 2024

Yeah, the wording of the warning here isn't clear. I took "non-overridable declarations" to mean methods that weren't overridden.

from swiftlint.

ZevEisenberg avatar ZevEisenberg commented on September 26, 2024

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.

tonyarnold avatar tonyarnold commented on September 26, 2024

"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.

ZevEisenberg avatar ZevEisenberg commented on September 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.