# Namespacing protocols to other types

**URL:** <https://forums.swift.org/t/namespacing-protocols-to-other-types/7328>\
**Category:** Discussion\
**Created:** [December 25, 2017, 2:53am UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328 "2017-12-25T02:53:33Z")\
**Posts on this page:** 16\
**Page:** 3

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [May 1, 2019, 9:05pm UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/41 "2019-05-01T21:05:12Z")

</div>

I have tweaked the type checker to allow that. I have also tweaked `getContextSubstitutions()` to substitute the types, although I am not sure if I have done that correctly.

```swift
protocol A {
  struct B {}
}

```

Inside IRGen, dumping the struct gives:

```swift
struct_decl range=[/Users/suyashsrijan/Desktop/test.swift:2:3 - line:2:13] "B" interface type='A.B.Type' access=internal non-resilient
  (constructor_decl implicit range=[/Users/suyashsrijan/Desktop/test.swift:2:10 - line:2:10] "init()" interface type='<Self where Self : A> (A.B.Type) -> () -> A.B' access=internal designated
    (parameter "self" interface type='A.B' inout)
    (parameter_list)
    (brace_stmt implicit
      (return_stmt implicit))))

```

I have also put `emitNestedTypeDecls(protocol->getMembers())` inside `emitProtocolDecl`, however it has trouble emitting the metadata for the struct (crashes in type converter). If you change the struct to a class, then the crash is in `useConformance` (because the protocol conformance ref is invalid).

Seems like I am missing something or the type substitution isn't working correctly.

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [May 2, 2019, 12:05pm UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/42 "2019-05-02T12:05:38Z")

</div>

The type of 'self' inside a method of A.B must not be A.B, but Self.B where Self is the _outer_ type parameter from the protocol A. However NominalType cannot model that right now.

I think a first step would be the planned changes to unify BoundGenericNominalType and NominalType, and change BoundGenericNominalType to store a substitution map instead of an array of generic arguments together with a parent type. Then the requisite changes to handle substitutions would be trivial.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [May 2, 2019, 8:37pm UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/43 "2019-05-02T20:37:37Z")

</div>

Hmm when will the planned changes happen? Is it worth waiting or could we just go with nested protocols first and build upon it later?

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [May 2, 2019, 9:33pm UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/44 "2019-05-02T21:33:30Z")

</div>

I don't think there are any concrete plans yet, but if you're interested, merging NominalType into BoundGenericNominalType would be a good first step.

And yeah, I think nesting protocols inside non-generic concrete types is much easier than the other direction and should not require any major representational changes.

---

<div class="post-metadata">

**Author:** ![BasemEmara](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/basememara/32/21528_2.png) [@BasemEmara](https://forums.swift.org/u/BasemEmara)\
**Post date:** [May 9, 2019, 10:48am UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/45 "2019-05-09T10:48:54Z")

</div>

This proposal has all our hearts desires about nesting protocols and the reverse: [Ease restrictions on protocol nesting by karwa · Pull Request #552 · apple/swift-evolution · GitHub](https://github.com/apple/swift-evolution/pull/552)

Unfortunately it hasn't moved since Swift 3, even though everyone seems to agree about it there. I don't know why it was closed either for no good reason. Can we re-open it and push it along further?

---

<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:** [July 31, 2019, 5:57am UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/46 "2019-07-31T05:57:18Z")

</div>

Hi @Karl, your PR hasn‘t moved forward for several month. I wonder if you still intend to push it forward or would you like someone else to take over it (maybe @suyashsrijan)?!

Thank you for the update in advance.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [July 31, 2019, 7:49am UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/47 "2019-07-31T07:49:47Z")

</div>

I have something working, just need to write some tests. I’ll try to do that in the next few days.

It’s pretty basic - just putting protocols inside other types, but that’s a good start and would be nice to get it merged first before we think about putting other types inside protocols (esp. since its related to generic protocols).

---

<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:** [July 31, 2019, 7:58am UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/48 "2019-07-31T07:58:50Z")

</div>

That is still great news. Looking forward to it.

---

<div class="post-metadata">

**Author:** ![benjamin.g](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@benjamin.g](https://forums.swift.org/u/benjamin.g)\
**Post date:** [January 21, 2021, 4:45pm UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/49 "2021-01-21T16:45:29Z")

</div>

Is this topic dead ? I still have the need for namespacing everytime my project starts to reach a certain size.

---

<div class="post-metadata">

**Author:** ![kirilltitov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kirilltitov/32/4182_2.png) [@kirilltitov](https://forums.swift.org/u/kirilltitov)\
**Post date:** [January 21, 2021, 5:08pm UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/50 "2021-01-21T17:08:58Z")

</div>

Everyone does. But any discussion inevitably sinks in "but what should we do with generics?" or something like that.

---

<div class="post-metadata">

**Author:** ![Ilias\_Karim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ilias_karim/32/2339_2.png) [@Ilias\_Karim](https://forums.swift.org/u/Ilias_Karim)\
**Post date:** [February 1, 2021, 10:01pm UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/51 "2021-02-01T22:01:48Z")

</div>

Modules already provide a good solution to namespacing.

---

<div class="post-metadata">

**Author:** ![Nobody1707](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nobody1707/32/2169_2.png) [@Nobody1707](https://forums.swift.org/u/Nobody1707)\
**Post date:** [February 2, 2021, 12:00am UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/52 "2021-02-02T00:00:38Z")

</div>

If that's true then why are there so many uninhabited enums used as namespaces floating around?

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [February 2, 2021, 5:19am UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/53 "2021-02-02T05:19:31Z")

</div>

They actually provide a fairly terrible solution. The only time they perform adequately is when using someone else's module. When writing your own (your app is a single module, by default), you might find that namespacing is useful for organization purposes, but that the overhead of a module is not worth the cost. This usually leads to language hacks such as uninhabited enums.

Personally, I have come across many instances where I had a collection of related static functions with which I did not want to pollute the global namespace. Also, I wanted it clear that the functions are indeed related. So I use uninhabited enums. I'm not going to create a module for half-a-dozen or fewer functions.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [February 2, 2021, 5:49pm UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/54 "2021-02-02T17:49:21Z")

</div>

Currently the best hack to allow nesting protocols inside of other types is to use a typealias. So given the common `MyView.Delegate` use-case, you could write something like:

```swift
protocol _MyViewDelegate: AnyObject {
}

struct MyView {
  typealias Delegate = _MyViewDelegate
}

class SomeDelegate: MyView.Delegate { // works!
}

```

But yeah, it would be _a lot_ nicer if we could write that the way we write other nested types. I also still really want nested types in protocols.

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [August 24, 2021, 4:00pm UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/56 "2021-08-24T16:00:49Z")

</div>

Karl's example has nothing to do with my use case, and your pejorative is unwarranted.

---

<div class="post-metadata">

**Author:** ![Ilias\_Karim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ilias_karim/32/2339_2.png) [@Ilias\_Karim](https://forums.swift.org/u/Ilias_Karim)\
**Post date:** [August 24, 2021, 7:55pm UTC](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328/60 "2021-08-24T19:55:43Z")

</div>

This looks like an excellent suggestion to me, cc: @Avi!

[Previous page](https://forums.swift.org/t/namespacing-protocols-to-other-types/7328.md?page=2)
