nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.77k stars 629 forks source link

opaqueGenericParameters cannot be disabled? #1533

Open tciuro opened 11 months ago

tciuro commented 11 months ago

I've added this to my .swiftformat file:

--someany false

However, the following code:

private func notifySubscribers<T: Codable>(of data: T) {

gets changed to the following, even if --someany false:

private func notifySubscribers(of data: some Codable) {

Am I missing some additional setting perhaps?

tciuro commented 11 months ago

And it's interesting that it didn't change this one:

protocol PusherManagerSubscriber {
    var id: UUID { get }
    func newEventHasArrived<T: Codable>(with object: T)
}

it should have been updated with this, right?

protocol PusherManagerSubscriber {
    var id: UUID { get }
    func newEventHasArrived(with object: some Codable)
}
nicklockwood commented 11 months ago

The --someany false option is just to prevent the rule replacing cases where T is untyped. To disable the rule altogether, use --disable opaqueGenericParameters

nicklockwood commented 11 months ago

And it's interesting that it didn't change this one:

That might be a bug. I'll investigate.

tciuro commented 11 months ago

Ah, cool. Thanks Nick!

nicklockwood commented 10 months ago

@tciuro OK, it's not a bug so much as a missing feature - right now opaqueGenericParameters doesn't apply to protocol methods. I'm not sure there's any good reason why it shouldn't but it would be a fairly big refactor to make that work so I'm going to treat it as a feature request.

nicklockwood commented 10 months ago

@calda can you see any potential problems with applying opaqueGenericParameters to protocol methods (besides implementation trickiness)?

tciuro commented 10 months ago

Thanks for looking into this, Nick!

calda commented 10 months ago

Ahh, this isn't applied to protocol requirements right now because protocol methods don't have a body?

I think updating this to apply to protocol should be a safe change. This compiles, which makes sense because the two syntaxes have the same meaning:

protocol PusherManagerSubscriber {
    func newEventHasArrived(with object: some Codable)
}

struct MyType: PusherManagerSubscriber {
  func newEventHasArrived<T: Codable>(with object: T) {
      print(object)
  }
}