# Private (not Protected)

**URL:** <https://forums.swift.org/t/private-not-protected/82740>\
**Category:** Discussion\
**Created:** [October 17, 2025, 3:57pm UTC](https://forums.swift.org/t/private-not-protected/82740 "2025-10-17T15:57:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![GiumaSoft](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/giumasoft/32/32182_2.png) [@GiumaSoft](https://forums.swift.org/u/GiumaSoft)\
**Post date:** [October 17, 2025, 3:57pm UTC](https://forums.swift.org/t/private-not-protected/82740/1 "2025-10-17T15:57:34Z")

</div>

**Why swift language still not allow private extension to be deployed on a separate file?**

I would like to ear some opinions about why that feature should not be available to developers. I know this is not a technical question, but in my heart I think most of you has felt same frustration of not having such a feature still today.

Example (not exhaustive of any possible cases)

MyView.swift:

```swift
struct MyView: View {
  var body: some View {
    _main_
  }
}

```

MyView+Extension.swift

```swift
private extension MyView {
  ///
  @ViewBuilder
  var _main_: some View {
    Text("Hellworld!")
  }
}

```

---

<div class="post-metadata">

**Author:** ![Sajjon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sajjon/32/12044_2.png) [@Sajjon](https://forums.swift.org/u/Sajjon)\
**Post date:** [October 17, 2025, 4:01pm UTC](https://forums.swift.org/t/private-not-protected/82740/2 "2025-10-17T16:01:04Z")

</div>

Can you specify an example you want to work which does not work today?

---

<div class="post-metadata">

**Author:** ![GiumaSoft](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/giumasoft/32/32182_2.png) [@GiumaSoft](https://forums.swift.org/u/GiumaSoft)\
**Post date:** [October 17, 2025, 4:18pm UTC](https://forums.swift.org/t/private-not-protected/82740/3 "2025-10-17T16:18:24Z")

</div>

i’ve updated topic with an example.

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [October 18, 2025, 3:28am UTC](https://forums.swift.org/t/private-not-protected/82740/4 "2025-10-18T03:28:43Z")

</div>

I am not able to reproduce this with Swift 6.2 (from Xcode 26.0):

```swift
% cat Sources/test/test.swift
struct Foo {
}

% cat Sources/test/extension.swift 
private extension Foo {
    var bar: String { "hello" }
}

% xcrun swift build
Building for debugging...
[5/5] Compiling test extension.swift
Build complete! (0.31s)

% xcrun swift --version      
swift-driver version: 1.127.14.1 Apple Swift version 6.2 (swiftlang-6.2.0.19.9 clang-1700.3.19.1)
Target: arm64-apple-macosx26.0

```

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [October 18, 2025, 4:14am UTC](https://forums.swift.org/t/private-not-protected/82740/5 "2025-10-18T04:14:25Z")

</div>

It was pointed out to me that the actual error occurs when you try to access the member declared in the extension:

```swift
struct Foo {
    func f () {
        self.bar // 'bar' is inaccessible due to 'fileprivate' protection level
    }

```

This is because on top-level declarations, `private` as an alias for `fileprivate`. I’ve seen it explained that the access modifier before `extension` is a shorthand for repeating that access modifier on all declarations within the extension, but this behavior is not consistent with that rule. But changing this behavior would risk reopening the `private`/`fileprivate` can of worms.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [October 18, 2025, 4:23am UTC](https://forums.swift.org/t/private-not-protected/82740/6 "2025-10-18T04:23:00Z")

</div>

> [@ksluder](#):
>
> I’ve seen it explained that the access modifier before `extension` is a shorthand for repeating that access modifier on all declarations within the extension, but this behavior is not consistent with that rule.

I'm not sure what you mean, your explanation is correct. `private extension` is equivalent to `fileprivate extension`, which is then applied to all the items in the extension, making it `fileprivate var bar`.

But I also agree, there should be a `typeprivate` equivalent. Or, to be consistent with many of my other suggestions `private(type)`. Which should also be enhanced for testing control with something like `private(type, test)`, and extended to other ACLs, like `package(type, test)`.

---

<div class="post-metadata">

**Author:** ![Sajjon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sajjon/32/12044_2.png) [@Sajjon](https://forums.swift.org/u/Sajjon)\
**Post date:** [October 18, 2025, 6:19am UTC](https://forums.swift.org/t/private-not-protected/82740/7 "2025-10-18T06:19:21Z")

</div>

As Jon later say, this is often called `typeprivate`, there are a few threads about this, eg:

> [@Implement Alvarez & Monteiro's "typeprivate", possibly with one or two extra restrictions](https://forums.swift.org/t/implement-alvarez-monteiros-typeprivate-possibly-with-one-or-two-extra-restrictions/40994):
>
> In 2016 there was this great proposal: [swift-evolution/NNNN-introduce-typeprivate-access-control-level.md at master · goncaloalvarez/swift-evolution · GitHub](https://github.com/goncaloalvarez/swift-evolution/blob/master/proposals/NNNN-introduce-typeprivate-access-control-level.md) The idea is to add "typeprivate" so you can put extensions into separate files to improve code readability without having to resort to "internal". (Currently any extensions that access private stored properties must all be in the file that declares the type, and "fileprivate" must be used. However this can lead to bloated code files.) If …

which links to formal proposal:

> <https://github.com/goncaloalvarez/swift-evolution/blob/master/proposals/NNNN-introduce-typeprivate-access-control-level.md>

And yes I’ve been missing this feature since Swift was created…

---

<div class="post-metadata">

**Author:** ![ksluder](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@ksluder](https://forums.swift.org/u/ksluder)\
**Post date:** [October 18, 2025, 6:47am UTC](https://forums.swift.org/t/private-not-protected/82740/8 "2025-10-18T06:47:33Z")

</div>

> [@Jon\_Shier](#):
>
> m not sure what you mean, your explanation is correct. `private extension` is equivalent to `fileprivate extension`, which is then applied to all the items in the extension, making it `fileprivate var bar`.

The mental model for `public extension` is that you can cut the `public` from before `extension` and paste it before every declaration within the extension:

```swift
/*public*/ extension Foo {
  public var bar: String { "hello" }
  public func baz() { print("hello") }
}

```

That mental model is broken with `private extension`.

> [@Jon\_Shier](#):
>
> But I also agree, there should be a `typeprivate` equivalent.

This is exactly the can of worms I was referring to.

---

<div class="post-metadata">

**Author:** ![GiumaSoft](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/giumasoft/32/32182_2.png) [@GiumaSoft](https://forums.swift.org/u/GiumaSoft)\
**Post date:** [October 18, 2025, 9:01am UTC](https://forums.swift.org/t/private-not-protected/82740/9 "2025-10-18T09:01:06Z")

</div>

> [@ksluder](#):
>
> This is exactly the can of worms I was referring to.

I don’t know which is the problem behind the scene u exactly refer with this phrase.

The [SE-0025](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0025-scoped-access-level.md) proposal his pre-history in the language evolution, the benefit of a **typeprivate** or **typeinternal** or **protected** swift keyword that apply access control at type level not file level meet the large demanding need of making order into massive chunks of code without loose the ability to hide properties and functions dev want to keep undisclosed.

But if the problem is clear the solution isn’t near, is there a way to put a petition in favor a new Access Level proposal that include type level protection like suggested by [Gonvalo\_Alvarez](https://forums.swift.org/t/swift-evolution-proposal-introduce-typeprivate-access-control-level/4610)

---

<div class="post-metadata">

**Author:** ![GiumaSoft](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/giumasoft/32/32182_2.png) [@GiumaSoft](https://forums.swift.org/u/GiumaSoft)\
**Post date:** [October 20, 2025, 11:21am UTC](https://forums.swift.org/t/private-not-protected/82740/10 "2025-10-20T11:21:16Z")

</div>

@John_McCall I read some of your tech answers and in my opinion you are a “Big Whale” in Swift. I’m interested in your opinion about the chance to have a **type level private access control** that overtake file scope limitations of current private.

---

<div class="post-metadata">

**Author:** ![ambient](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ambient/32/28443_2.png) [@ambient](https://forums.swift.org/u/ambient)\
**Post date:** [October 21, 2025, 1:32pm UTC](https://forums.swift.org/t/private-not-protected/82740/11 "2025-10-21T13:32:32Z")

</div>

I’d love to see a `typeprivate` keyword

---

<div class="post-metadata">

**Author:** ![jpmhouston](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jpmhouston/32/4045_2.png) [@jpmhouston](https://forums.swift.org/u/jpmhouston)\
**Post date:** [October 24, 2025, 7:56am UTC](https://forums.swift.org/t/private-not-protected/82740/12 "2025-10-24T07:56:00Z")

</div>

> [@ambient](#):
>
> I’d love to see a `typeprivate` keyword

I'd love to see that `typeprivate` keyword to be spelled `private`.

---

<div class="post-metadata">

**Author:** ![GiumaSoft](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/giumasoft/32/32182_2.png) [@GiumaSoft](https://forums.swift.org/u/GiumaSoft)\
**Post date:** [October 24, 2025, 9:54am UTC](https://forums.swift.org/t/private-not-protected/82740/13 "2025-10-24T09:54:41Z")

</div>

I absolutely agree, it make sense. For me it can be called 30 chars long keyword if that goal can be reached as fast as possible.
