realm / SwiftLint

A tool to enforce Swift style and conventions.
https://realm.github.io/SwiftLint
MIT License
18.61k stars 2.22k forks source link

[Question] static_operator with infix #5656

Closed NikKovIos closed 2 months ago

NikKovIos commented 2 months ago
image image

Can't figure out how to declare infix operator without warning. Help please.

SimplyDanny commented 2 months ago

This should work:

infix operator +?

extension Int {
  static func +? (a: Self?, b: Self) -> Self? {
    if let a { a + b } else { nil }
  }
}

The idea is that operators should be static and associated with the type. But the operator declaration needs to go to file scope. It cannot be part of the extension.

NikKovIos commented 2 months ago

Work!