# Is it possible to use a protocol to require a nested struct?

**URL:** <https://forums.swift.org/t/is-it-possible-to-use-a-protocol-to-require-a-nested-struct/43412>\
**Category:** Using Swift\
**Created:** [December 29, 2020, 4:12am UTC](https://forums.swift.org/t/is-it-possible-to-use-a-protocol-to-require-a-nested-struct/43412 "2020-12-29T04:12:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![DandyLyons](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dandylyons/32/14417_2.png) [@DandyLyons](https://forums.swift.org/u/DandyLyons)\
**Post date:** [December 29, 2020, 4:12am UTC](https://forums.swift.org/t/is-it-possible-to-use-a-protocol-to-require-a-nested-struct/43412/1 "2020-12-29T04:12:31Z")

</div>

Is it possible to use a protocol to require a nested struct?

I have a struct like so:

```swift
struct DraftsURL {
    static let scheme = "drafts5"
    static let host = "x-callback-url"
    struct actions {
        static let create = "/create"
        static let open = "/open"
        static let get = "/get"
        static let prepend = "/prepend"
        static let append = "/append"
        static let replaceRange = "/replaceRange"
        static let search = "/search"
        static let workspace = "/workspace"
        static let runAction = "/runAction"
        static let dictate = "/dictate"
        static let arrange = "/arrange"
    }
}

```

I'd like to generalize some of this with a protocol so that other structs can do the same thing. Something like this:

```swift
protocol URLBuilder {
    static var scheme: String { get }
    static var host: String { get }
    struct actions { } //Type 'actions' cannot be nested in protocol 'URLBuilder'
}

```

For my purposes, I just want to require a struct called actions without dictating anything else in the implementation of that struct.

So is it possible to use a protocol to require a nested struct?

Or perhaps am I not doing this in the "right" way? Is this a sign I should be using a class instead?

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [December 29, 2020, 4:20am UTC](https://forums.swift.org/t/is-it-possible-to-use-a-protocol-to-require-a-nested-struct/43412/2 "2020-12-29T04:20:46Z")

</div>

It sounds like you want `associatedtype`

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [December 29, 2020, 5:48am UTC](https://forums.swift.org/t/is-it-possible-to-use-a-protocol-to-require-a-nested-struct/43412/3 "2020-12-29T05:48:31Z")

</div>

Why is it a struct with a lowercase plural name instead of an enum named `Action` though?

If you do what Nevin said, any types will work.

---

<div class="post-metadata">

**Author:** ![DandyLyons](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dandylyons/32/14417_2.png) [@DandyLyons](https://forums.swift.org/u/DandyLyons)\
**Post date:** [December 29, 2020, 8:27am UTC](https://forums.swift.org/t/is-it-possible-to-use-a-protocol-to-require-a-nested-struct/43412/4 "2020-12-29T08:27:22Z")

</div>

Thank you guys. I’ll try both of those.

---

<div class="post-metadata">

**Author:** ![DandyLyons](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dandylyons/32/14417_2.png) [@DandyLyons](https://forums.swift.org/u/DandyLyons)\
**Post date:** [December 29, 2020, 9:23pm UTC](https://forums.swift.org/t/is-it-possible-to-use-a-protocol-to-require-a-nested-struct/43412/5 "2020-12-29T21:23:53Z")

</div>

So an enum sounds more type safe than just an `associatedtype` (since `associatedtype` will accept any type). But I'm still playing around with enums inside of protocols and I haven't figured out how to nest an `enum` inside a protocol since Types are not allowed to be nested in protocols.

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [December 29, 2020, 9:30pm UTC](https://forums.swift.org/t/is-it-possible-to-use-a-protocol-to-require-a-nested-struct/43412/6 "2020-12-29T21:30:12Z")

</div>

You're allowed to have `typealiases` inside protocols, so you could do:

```swift
enum URLBuilderAction { ... }

protocol URLBuilder {
  typealias Action = URLBuilderAction
  ...
}
```

---

<div class="post-metadata">

**Author:** ![DandyLyons](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dandylyons/32/14417_2.png) [@DandyLyons](https://forums.swift.org/u/DandyLyons)\
**Post date:** [December 30, 2020, 2:38am UTC](https://forums.swift.org/t/is-it-possible-to-use-a-protocol-to-require-a-nested-struct/43412/7 "2020-12-30T02:38:30Z")

</div>

Hmm, I'm not sure this works either. If I understand correctly, then with that approach the enum is already statically defined outside of the protocol. With a typealias now that enum is already defined, plus I don't think swift will let me `extension` the enum.

I'm trying to figure out how to say in the protocol:

> "I'd like some enum named `Action`, but you can fill that enum with whatever you want."

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [December 30, 2020, 5:06am UTC](https://forums.swift.org/t/is-it-possible-to-use-a-protocol-to-require-a-nested-struct/43412/8 "2020-12-30T05:06:40Z")

</div>

Why does it matter that it's an enum? There's no way to require that, and if you could, there would be nothing you could do with that restriction anyway.

---

<div class="post-metadata">

**Author:** ![DandyLyons](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dandylyons/32/14417_2.png) [@DandyLyons](https://forums.swift.org/u/DandyLyons)\
**Post date:** [December 30, 2020, 9:34pm UTC](https://forums.swift.org/t/is-it-possible-to-use-a-protocol-to-require-a-nested-struct/43412/9 "2020-12-30T21:34:26Z")

</div>

I was trying to implement your advice when you said:

> [@anon9791410](#):
>
> Why is it a struct with a lowercase plural name instead of an enum named `Action` though?
> 
> If you do what Nevin said, any types will work.

---

<div class="post-metadata">

**Author:** ![Peter-Schorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/peter-schorn/32/11183_2.png) [@Peter-Schorn](https://forums.swift.org/u/Peter-Schorn)\
**Post date:** [January 21, 2021, 4:14pm UTC](https://forums.swift.org/t/is-it-possible-to-use-a-protocol-to-require-a-nested-struct/43412/10 "2021-01-21T16:14:55Z")

</div>

No, you can not create a protocol that requires conforming types to have nested types defined inside of them. Why would you need this? Do you have an example use case?
