# Constrain protocol usage outside the module

**URL:** <https://forums.swift.org/t/constrain-protocol-usage-outside-the-module/8832>\
**Category:** Pitches\
**Created:** [January 25, 2018, 9:03am UTC](https://forums.swift.org/t/constrain-protocol-usage-outside-the-module/8832 "2018-01-25T09:03:18Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [January 25, 2018, 10:13am UTC](https://forums.swift.org/t/constrain-protocol-usage-outside-the-module/8832/3 "2018-01-25T10:13:07Z")

</div>

> [@xwu](#):
>
> This does not make sense in the context of the larger idea. If you can create protocol Q : P and then conform your own type to Q, then it necessarily conforms to P.

Maybe my wording wasn't quite correct there, but I showed in the example what I really meant. You can refine public-but-closed protocol, but you cannot conform your own types to nether the public-but-closed protocol nor to your refinement, the only conformance you're allowed to make is to a type which already conforms to that protocols - which is a type from where that protocol was defined. You can only conform your types to open protocols.

> [@xwu](#):
>
> At this stage in Swift, I don’t think that this is realistic. I think whatever the exhaustive enum conversation settles on (frozen?) will likely be how future additions to the language such as this would proceed.

Why? As long a we do it in respect of source compatibility that should be fine, or are you saying that we cannot evolve the language any further, and we should not fix areas that couldn't be fixed due the lack of engineering time to implement things in time? I think @tkremenek mentioned that in a different topic that we should still be able to evolve the language even if the ABI is closed, but I still get the impression from such replies as yours now that the ship has sailed for the language to evolve any further.

> [@xwu](#):
>
> These protocols are underscored for varying reasons.
> 
> Which brings me to an important question: What are some concrete use cases which motivate you to bring this up?

Funny that you ask. Wasn't it you who said long time ago that to solve a particular problem I described we can use enums, to be more precise `anonymous enums cases`?

```swift
// The example was more or less like this
protocol CustomType {
  var instance: IntOrString { get }
}

enum IntOrString {
  case int(Int)
  case string(String) 
}

```

But now that we know this isn't going to happen at all because we don't want `Int | String` in the type-system you can no longer argue with that solution against closed protocols. Closed protocols can be used to disallow the user from conforming to a protocol which might be used to hide concrete types from the outside the module for example (isn't Apple also doing this quite often in theirs frameworks? - there are some protocols which you cannot conform to because you cannot instantiate or get all types it requires due to hidden `init`). I think it's fair to say that the library design is up to developers and should not restricted by language design, especially not when we already have a similar behavior in the language. This will become a generalization of that feature.

One could introduce `@closed public protocol` for Swift 4 which in Swift 5 mode would mean `public protocol` and `public protocol` without `@closed` from Swift 4 would mean `open protocol` in Swift 5 mode. Isn't the source compatibility save here?

---

_[View the full topic](https://forums.swift.org/t/constrain-protocol-usage-outside-the-module/8832)._
