# Syntactic Sugar for nested types in extensions

**URL:** <https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299>\
**Category:** Discussion\
**Tags:** syntax\
**Created:** [March 23, 2018, 11:05am UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299 "2018-03-23T11:05:40Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjstelling](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rjstelling/32/1475_2.png) [@rjstelling](https://forums.swift.org/u/rjstelling)\
**Post date:** [March 23, 2018, 11:05am UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299/1 "2018-03-23T11:05:40Z")

</div>

> **Preamble:** I had this idea while writing a lot of nested types, at first it seems like a great idea that reduced ["Pyramid of doom"](https://en.wikipedia.org/wiki/Pyramid_of_doom_(programming)) type issues.
> 
> I've since cooled on the idea, but I'm still on the fence and wanted to get some community feedback.

Currently nested types can be defined within `extension`s like so;

```
struct TopLevel {}

extension TopLevel {
    struct SecondLevel {}
}

extension TopLevel.SecondLevel {
    struct ThirdLevel {}
}

```

To reduce the nested indentation we could introduce shorthand like this;

```
extension struct TopLevel.SecondLevel.ThirdLevel.FinalLevel {
    // a struct defined as if we inside the ThirdLevel extension 
}

```

Obviously this would also extend to `class` and `enum`, e.g;

```
extension enum TopLevel.SecondLevel.ThirdLevel.FinalLevel.Colours: String {
    case red
    case green
    case blue
}

```

Would it make code less readable? Is the deeply nested type pattern common enough to make such sugar useful in the average code base?

Rich

---

<div class="post-metadata">

**Author:** ![DeFrenZ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/defrenz/32/827_2.png) [@DeFrenZ](https://forums.swift.org/u/DeFrenZ)\
**Post date:** [March 23, 2018, 11:35am UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299/2 "2018-03-23T11:35:33Z")

</div>

I would like to do something like this, but I wouldn't require `extension`. If I do `struct TopLevel.SecondLevel { ... }` it could already check if there's a `TopLevel` somewhere and extend it automatically. I think it's fine to omit `extension` in this case as we're not really changing the type, we're only using its namespace.

---

<div class="post-metadata">

**Author:** ![GarthSnyder](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/garthsnyder/32/969_2.png) [@GarthSnyder](https://forums.swift.org/u/GarthSnyder)\
**Post date:** [March 23, 2018, 8:01pm UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299/3 "2018-03-23T20:01:22Z")

</div>

Just to be clear, the alternative to

```swift
extension struct TopLevel.SecondLevel.ThirdLevel.FinalLevel {
    // a struct defined as if we inside the ThirdLevel extension 
}

```

is the currently-working

```swift
extension TopLevel.SecondLevel.ThirdLevel {
    struct FinalLevel {}
}

```

Is that right? I'm not sure I see the syntactic benefit to the alternate form. The current syntax is just as clear and concise.

Yes, the current system makes you write out some boilerplate-looking structure when you're nested four layers deep in structs that have no members and don't do anything. But in real code, each of these layers would have actual content and the structuring would not be unwelcome. Besides, there's nothing to stop you from writing it all out as one extension anyway...

---

<div class="post-metadata">

**Author:** ![jarod](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jarod/32/364_2.png) [@jarod](https://forums.swift.org/u/jarod)\
**Post date:** [March 23, 2018, 8:20pm UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299/4 "2018-03-23T20:20:32Z")

</div>

I've wanted this syntax for a long time (without the `extension` prefix as @DeFrenZ mentioned). For me it's mostly a matter of indentation consistency. It's somewhat unfortunate that the body of struct `A` gets indented one level while struct `A.B` gets indented two levels. Not a critical issue, but it would be nice to allow top-level and nested types to be symmetrical in this sense, and I don't see any real downside to it.

---

<div class="post-metadata">

**Author:** ![hlovatt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hlovatt/32/3044_2.png) [@hlovatt](https://forums.swift.org/u/hlovatt)\
**Post date:** [March 25, 2018, 1:10am UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299/5 "2018-03-25T01:10:18Z")

</div>

Personally I use:

```swift
struct TopLevel {
    fileprivate func somethingIWantAccessTo() { ... }
}

struct FirstLevel {
    private let topLevel: TopLevel
    func somethingThatUsesTopLevel() {
        topLevel.somethingIwantAccessTo()
        ....
    }
}

```

Therefore can't see value in this change - sorry.

---

<div class="post-metadata">

**Author:** ![anthonylatsis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anthonylatsis/32/39232_2.png) [@anthonylatsis](https://forums.swift.org/u/anthonylatsis)\
**Post date:** [March 25, 2018, 1:57am UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299/6 "2018-03-25T01:57:35Z")

</div>

Apparently `TopLevel` and `FirstLevel` in your example aren't semantically and structurally related whatsoever, which means there isn't even a need for nesting them – the main problem discussed in this thread. That said, what's the point of your example?

---

<div class="post-metadata">

**Author:** ![hlovatt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hlovatt/32/3044_2.png) [@hlovatt](https://forums.swift.org/u/hlovatt)\
**Post date:** [March 25, 2018, 4:47am UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299/7 "2018-03-25T04:47:30Z")

</div>

Oops top level is a class - makes sense then - acts like Java inner class but avoids deep nesting with can be hard to follow at the expense of holding a pointer.

---

<div class="post-metadata">

**Author:** ![anthonylatsis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anthonylatsis/32/39232_2.png) [@anthonylatsis](https://forums.swift.org/u/anthonylatsis)\
**Post date:** [March 25, 2018, 1:02pm UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299/8 "2018-03-25T13:02:19Z")

</div>

I believe you misunderstood. This has nothing to do with the construct (`class`, `struct`, etc) you use. Doing something like this...

> [@hlovatt](#):
>
> ```swift
> struct TopLevel {
> fileprivate func somethingIWantAccessTo() { ... }
> }
> 
> struct FirstLevel {
> private let topLevel: TopLevel
> func somethingThatUsesTopLevel() {
> topLevel.somethingIwantAccessTo()
> ....
> }
> }
> 
> ```

..instead of this...

```swift
struct TopLevel {
    fileprivate func somethingIWantAccessTo() { ... }

    struct FirstLevel {
        private let topLevel: TopLevel

        func somethingThatUsesTopLevel() {
            topLevel.somethingIWantAccessTo()
            ...
        }
    }
}

```

...isn't even a workaround. It is as wrong as it would be to make `Collection.Indices` into simply `Indices`. Right now, the only correct method to semantically and structurally express the belonging of one type to another is nesting them or extending one. The whole point of such a relationship is for the nested type to be referred to as `A.B` outside the type it belongs to and simply as `B` within. Properties and methods act under the same rules. This thread proposes a usage convenience for _this concept_.

---

<div class="post-metadata">

**Author:** ![anthonylatsis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anthonylatsis/32/39232_2.png) [@anthonylatsis](https://forums.swift.org/u/anthonylatsis)\
**Post date:** [March 25, 2018, 1:13pm UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299/9 "2018-03-25T13:13:30Z")

</div>

However, considering [this](https://bugs.swift.org/browse/SR-631) bug is _still_ present, the chances of the proposal being accepted and implemented are very low.

---

<div class="post-metadata">

**Author:** ![rjstelling](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rjstelling/32/1475_2.png) [@rjstelling](https://forums.swift.org/u/rjstelling)\
**Post date:** [March 25, 2018, 3:38pm UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299/10 "2018-03-25T15:38:15Z")

</div>

This is as about a good argument **for** as I can see.

And there are few (in any) drawbacks, that I can see.

---

<div class="post-metadata">

**Author:** ![rjstelling](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rjstelling/32/1475_2.png) [@rjstelling](https://forums.swift.org/u/rjstelling)\
**Post date:** [March 25, 2018, 3:57pm UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299/11 "2018-03-25T15:57:16Z")

</div>

I don’t know how I’ve not encountered that bug!

Seems like we’re getting to a point int Swift’s maturity where bugs like that will start to be addressed.

It might however be a blocker for any implementation of this feature.

---

<div class="post-metadata">

**Author:** ![rjstelling](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rjstelling/32/1475_2.png) [@rjstelling](https://forums.swift.org/u/rjstelling)\
**Post date:** [March 30, 2018, 8:32am UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299/12 "2018-03-30T08:32:08Z")

</div>

It seems like this concept has its supporters, and it’s detractors fall mainly in the camp of, “I wouldn’t use it” or “is it really useful?”.

I’ve not heard any philosophical arguments against it. And more importantly no technical reasons why it wouldn’t work.

If there are no major objections or technical areas we haven’t considered, I will start to investigate the feasibility of implementing it for a formal proposal.

---

<div class="post-metadata">

**Author:** ![tayyab](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tayyab/32/2929_2.png) [@tayyab](https://forums.swift.org/u/tayyab)\
**Post date:** [March 30, 2018, 9:54am UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299/13 "2018-03-30T09:54:58Z")

</div>

I think `extension` shouldn't be written in the beginning.

---

<div class="post-metadata">

**Author:** ![rjstelling](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rjstelling/32/1475_2.png) [@rjstelling](https://forums.swift.org/u/rjstelling)\
**Post date:** [March 30, 2018, 10:37am UTC](https://forums.swift.org/t/syntactic-sugar-for-nested-types-in-extensions/11299/14 "2018-03-30T10:37:00Z")

</div>

Yes, I agree.

It’s better categorised as some this like, “Inferring extensions when when creating nested types”
