Warning for vacuously complying to an all-optional protocol.


(William Shipley) #1

The Swift 2.2 manual says:

NOTE

Strictly speaking, you can write a custom class that conforms to CounterDataSource without implementing either protocol requirement. They are both optional, after all. Although technically allowed, this wouldn’t make for a very good data source.

I think it’d be helpful to get a compiler warning when you declare that a class or struct complies to a protocol (that only has optional methods) but doesn’t actually implement any of the protocol.

E.G., this show show a warning:

import Cocoa

class kindaUseless: NSObject, NSTableViewDelegate {
}

While this behavior is mathematically consistent, it’s not rational and doesn’t really fit in with Swift.

Extraneous protocol conformance is messy and I can’t imagine a scenario in which it’s actually useful.

Thoughts?

-W


(Douglas Gregor) #2

It should be a straightforward compiler warning to add. Want to file a bug at bugs.swift.org <http://bugs.swift.org/>? It would make a good starter bug.

  - Doug

···

On Apr 25, 2016, at 3:37 PM, William Shipley via swift-evolution <swift-evolution@swift.org> wrote:

The Swift 2.2 manual says:

NOTE

Strictly speaking, you can write a custom class that conforms to CounterDataSource without implementing either protocol requirement. They are both optional, after all. Although technically allowed, this wouldn’t make for a very good data source.

I think it’d be helpful to get a compiler warning when you declare that a class or struct complies to a protocol (that only has optional methods) but doesn’t actually implement any of the protocol.

E.G., this show show a warning:

import Cocoa

class kindaUseless: NSObject, NSTableViewDelegate {
}

While this behavior is mathematically consistent, it’s not rational and doesn’t really fit in with Swift.

Extraneous protocol conformance is messy and I can’t imagine a scenario in which it’s actually useful.

Thoughts?