Constrain protocol usage outside the module

The part I’m most interested in is that @closed disallows consumers of the module from conforming their own types to a protocol. This, in turn, means that we can generate an exhaustive list of all types that conform to a certain protocol since they’re all within the module. Therefore, we can perform an exhaustive switch over those types.

switch intOrString {
  case let int as Int: print(int*int)
  case let str as String: print(“\(str) squared”)
  // no default necessary & will get a non-exhaustive error if we conform another type to IntOrString in the future
}

EDIT: of course this exhaustive switching will only work within the module, clients still need to be resilient to potential future changes in the module they’re consuming

3 Likes