# Cannot extend private local type

**URL:** <https://forums.swift.org/t/cannot-extend-private-local-type/66476>\
**Category:** Using Swift\
**Tags:** extensions\
**Created:** [July 31, 2023, 12:29pm UTC](https://forums.swift.org/t/cannot-extend-private-local-type/66476 "2023-07-31T12:29:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![couchdeveloper](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/couchdeveloper/32/23601_2.png) [@couchdeveloper](https://forums.swift.org/u/couchdeveloper)\
**Post date:** [July 31, 2023, 12:29pm UTC](https://forums.swift.org/t/cannot-extend-private-local-type/66476/1 "2023-07-31T12:29:46Z")

</div>

Having a top-level private class, for example:

```swift
private final class Bar {}

```

then, I'm able to extend it in an extension (in the same file), for example:

```swift
extension Bar {
    static func baz() {}
}

```

However, when I have a local private class:

```swift
enum Foo {
     private final class Bar {}
}

```

and try to extend it (in the same file):

```swift
private extension Foo.Bar {
    static func baz() {}
}

```

The compiler emits the error

```swift
private extension Foo.Bar 
                      ^ 'Bar' is inaccessible due to 'private' protection level.

```

A use case would be when using the Observation module (from Version 15.0 beta 5 (15A5209g)), where for example a "Model" class, has been put into some name space, say "MyFeature":

```swift
import Observation

enum MyFeature { 

    @Observable
    private final class Model {
        ... 
    }
}

```

This wont compile, since the expansion of macro `@Observable` adds an extension (in file scope) like:

```swift
extension MyFeature.Model: Observation.Observable {}

```

and the compiler complains. The extension should also be private, if I'm correct? But anyway, it seems, this is a language issue.

Well, one could of course change the accessibility of the class `Model` to `internal` or `public` – but, IMHO, this would be against best practices, if the symbol "Model" would and should be only visible in this file.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [July 31, 2023, 12:35pm UTC](https://forums.swift.org/t/cannot-extend-private-local-type/66476/2 "2023-07-31T12:35:44Z")

</div>

> [@couchdeveloper](#):
>
> However, when I have a local private class:
> 
> ```swift
> enum Foo {
> private final class Bar {}
> }
> 
> ```

Here, `private` refers to the lexical scope _inside_ `Foo`. Since extensions are always at file scope, you've explicitly made `Foo.Bar` inaccessible for extension by using `private`.

If you want to be able to extend `Foo.Bar` from file scope, then it needs to be at least `fileprivate`.

> [@couchdeveloper](#):
>
> Well, one could of course change the accessibility of the class `Model` to `internal` or `public` – but, IMHO, this would be against best practices, if the symbol "Model" would and should be only visible in this file.

Based on your description of what visibility you want ("only visible in this file"), it sounds like `fileprivate` is explicitly what you'd want.

If you want to avoid using `fileprivate` explicitly, you can also accomplish the behavior by declaring `Bar` in a `private extension` of `Foo`, since again extensions are at file scope:

```swift
enum Foo { ... }
private extension Foo {
  final class Bar { }
}
extension Foo.Bar { ... }

```

---

<div class="post-metadata">

**Author:** ![couchdeveloper](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/couchdeveloper/32/23601_2.png) [@couchdeveloper](https://forums.swift.org/u/couchdeveloper)\
**Post date:** [July 31, 2023, 12:40pm UTC](https://forums.swift.org/t/cannot-extend-private-local-type/66476/3 "2023-07-31T12:40:52Z")

</div>

This fixes the issue. However, how is a symbol defined in a module different than when it is defined in a local scope? A global _private_ definition can be extended. Shouldn't this yield the same compiler error, unless the global symbol is declared `fileprivate` as well:

```swift
fileprivate final class Bar {}

extension Bar {
    static func baz() {}
}

```

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [July 31, 2023, 12:42pm UTC](https://forums.swift.org/t/cannot-extend-private-local-type/66476/4 "2023-07-31T12:42:54Z")

</div>

> [@couchdeveloper](#):
>
> A global _private_ definition can be extended. Shouldn't this yield the same compiler error, unless the global symbol is declared `fileprivate` as well:

Swift's access control modifiers are _lexical_: `private` at file scope (it's not "global") means it is private to the file—i.e., `fileprivate` is identical to `private` in that position.

---

<div class="post-metadata">

**Author:** ![couchdeveloper](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/couchdeveloper/32/23601_2.png) [@couchdeveloper](https://forums.swift.org/u/couchdeveloper)\
**Post date:** [July 31, 2023, 12:46pm UTC](https://forums.swift.org/t/cannot-extend-private-local-type/66476/5 "2023-07-31T12:46:38Z")

</div>

OK, this explains it.  
Thanks a lot!
