27

In addition to this syntax with a protocol extension:

protocol P {}
extension P where Self : UIView {}

... I discovered by accident that you can use the same where clause on the protocol itself:

protocol P where Self : UIView {}

Notice that this is not the same as a where clause constraining a generic protocol, and does not itself make P a generic protocol.

My experiments seem to show that only a colon can be used here, and the thing after the colon must be a class or a protocol (which may be generic).

I became curious: how did this escape my notice? So I went hunting for evidence of when it arose. In Swift 3.0, the former syntax is legal but not the latter. In Swift 3.3, both are legal. So the latter syntax must have been quietly introduced in something like Swift 3.2. I say "quietly" because I can't find anything about it in the release notes.

What is the second syntax for? Is it, as it appears, just a convenient way of making sure no other type can adopt this protocol? The Swift headers do not seem to make any use of it.

4
  • 6
    Being able to say where Self : UIView in a protocol declaration was accidental consequence of SE-0156 – the feature itself hasn't been fully implemented yet (really the compiler should have rejected the syntax until ready). It currently has lots of sharp edges around it, so I would steer clear of it for now – see stackoverflow.com/a/50647762/2976878.
    – Hamish
    Jun 18, 2018 at 16:02
  • 4
    @Hamish That comment is good enough to be the answer, imo
    – Alexander
    Jun 18, 2018 at 16:03
  • @Alexander Hmm, it does mostly duplicate the stuff I already say in the linked answer, but I guess it's more accessible as an answer to this question... yeah okay I'll write an answer :)
    – Hamish
    Jun 18, 2018 at 16:06
  • Possibly related topic in the Swift forum: What happens when a protocol is constrained to inherit from a class?
    – Martin R
    Jun 19, 2018 at 8:42

1 Answer 1

29

The ability to put superclass constraints on protocols declarations (that is, being able to define protocol P where Self : C where C is the type of a class) was a premature consequence of
SE-0156, and the syntax should have been rejected in Swift 4.x until the feature was implemented. Attempting to use this feature in Swift 4.x can cause miscompilation and crashes, so I would avoid using it until Swift 5.

In Swift 5 (Xcode 10.2) the feature has now been implemented. From the release notes:

Protocols can now constrain their conforming types to those that subclass a given class. Two equivalent forms are supported:

protocol MyView: UIView { /*...*/ }
protocol MyView where Self: UIView { /*...*/ } 

Swift 4.2 accepted the second form, but it wasn’t fully implemented and could sometimes crash at compile time or runtime. (SR-5581) (38077232)

This syntax places a superclass constraint on MyView which restricts conforming types to those inheriting from (or being) UIView. In addition, the usage of MyView is semantically equivalent to a class existential (e.g UIView & MyView) in that you can access both members of the class and requirements of the protocol on the value.

For example, expanding upon the release notes' example:

protocol MyView : UIView {
  var foo: Int { get }
}

class C : MyView {} // error: 'P' requires that 'C' inherit from 'UIView'

class CustomView : UIView, MyView {
  var foo: Int = 0
}

// ...

let myView: MyView = CustomView(frame: .zero)

// We can access both `UIView` members on a `MyView` value
print(myView.backgroundColor as Any)

// ... and `MyView` members as usual.
print(myView.foo)
7
  • 1
    protocol P : UIView {} I hope not!
    – matt
    Jul 7, 2018 at 10:19
  • @matt Yup: github.com/apple/swift/pull/17816/…
    – Hamish
    Jul 7, 2018 at 10:21
  • IMO the change makes sense – protocol derivations (i.e protocol P2 : P1) should really be semantically equivalent to constraints on Self, i.e protocol P2 where Self : P1. So if you allow protocol P2 where Self : SomeClass, it seems logical to also allow protocol P2 : SomeClass (that being said, I do appreciate how it could be easily mistaken for inheritance).
    – Hamish
    Jul 7, 2018 at 10:25
  • Anyway to protocol P where Self : C & Self: Decodable{}? EDIT: Nevermind. I can use typealias to combine C & Decodable and the constrain Self to that alias type...
    – mfaani
    Dec 28, 2018 at 10:51
  • @Honey In Swift 5 you would write protocol P where Self : C, Self : Decodable {} or protocol P where Self : C & Decodable {} or protocol P : C, Decodable {}. In Swift 4.x however, as discussed in my answer, such a construct isn't supported by the compiler.
    – Hamish
    Dec 28, 2018 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.