# \[Pitch\] New access control for access inside type from other files

**URL:** <https://forums.swift.org/t/pitch-new-access-control-for-access-inside-type-from-other-files/54051>\
**Category:** Pitches\
**Created:** [December 14, 2021, 5:06pm UTC](https://forums.swift.org/t/pitch-new-access-control-for-access-inside-type-from-other-files/54051 "2021-12-14T17:06:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![oren](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/oren/32/20901_2.png) [@oren](https://forums.swift.org/u/oren)\
**Post date:** [December 14, 2021, 5:06pm UTC](https://forums.swift.org/t/pitch-new-access-control-for-access-inside-type-from-other-files/54051/1 "2021-12-14T17:06:56Z")

</div>

Apologies in advance if such a thing had already been proposed.

Sometimes, when constructing types, we separate some functionality and encapsulate it within different extensions (Delegate conformance, specific context responsibility etc.).  
We often prefer also separating this functionality into separate files for better readability.

This works well until we need to access a property we want to "hide" from the outside world to express it shouldn't be accessed directly.  
We can use `private` or `fileprivate`, but then we lose the ability to access it from a different file.

What I propose is a new keyword which makes properties and functions marked with it accessible only within this type.

For example:

`MyType.swift`

```swift
class MyType {
    member var myString: String = ""
    ....
}

```

And then, in a separate file:  
`MyType+Delegate.swift`

```swift
extension MyType: MyDelegate {
    func foo() {
        myString = "bar"
    }
}

```

Trying to access `myString` from outside the type causes a compilation error.

Of course, the keyword `member` is just a suggestion, and can be changed.  
Other options: `context`, `domain`, `typemember`.

This is my first proposal, so I hope it's detailed sufficiently. I'd appreciate any input you may have.

---

<div class="post-metadata">

**Author:** ![oren](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/oren/32/20901_2.png) [@oren](https://forums.swift.org/u/oren)\
**Post date:** [December 15, 2021, 6:29am UTC](https://forums.swift.org/t/pitch-new-access-control-for-access-inside-type-from-other-files/54051/2 "2021-12-15T06:29:07Z")

</div>

After giving it some more thought, I believe a better name for this would be ‘typeprivate’, which keeps it more consistent with the current ‘fileprivate’.

---

<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 15, 2021, 7:08am UTC](https://forums.swift.org/t/pitch-new-access-control-for-access-inside-type-from-other-files/54051/3 "2021-12-15T07:08:36Z")

</div>

Using `type` is inaccurate because if we get this, it needs to apply to protocols as well.

`protected` is the standard. It was apparently [in the works for C# 8 interfaces (what they call protocols)](https://jeremybytes.blogspot.com/2019/11/c-8-interfaces-public-private-and.html), for example, but as far as I know, it didn't happen.

---

<div class="post-metadata">

**Author:** ![technogen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technogen/32/45489_2.png) [@technogen](https://forums.swift.org/u/technogen)\
**Post date:** [December 15, 2021, 1:55pm UTC](https://forums.swift.org/t/pitch-new-access-control-for-access-inside-type-from-other-files/54051/4 "2021-12-15T13:55:47Z")

</div>

I believe, that’s very unlikely to ever happen because access scopes in Swift are purely lexical by design. I think the same problem is much better solved by access-controlled protocol conformances.

---

<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:** [December 15, 2021, 7:38pm UTC](https://forums.swift.org/t/pitch-new-access-control-for-access-inside-type-from-other-files/54051/5 "2021-12-15T19:38:38Z")

</div>

There are 80+ results on these forums, reflecting more than five years of prior discussion, on this topic when one searches for “protected access”:

[https://forums.swift.org/search?context=topic&context\_id=54051&q=protected%20access&skip\_context=true](https://forums.swift.org/search?context=topic&context_id=54051&q=protected%20access&skip_context=true)

Not sure there’s much left to say which hasn’t already been said, but by all means if there’s some new insight that’s been overlooked then do share it here!

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [December 15, 2021, 10:19pm UTC](https://forums.swift.org/t/pitch-new-access-control-for-access-inside-type-from-other-files/54051/6 "2021-12-15T22:19:20Z")

</div>

This is a very frequently made suggestion, e.g. [the latest example](https://forums.swift.org/t/calling-private-methods-from-extensions-in-separate-files/).

The frequency of this feature request suggests there's some room for improvement, at least for those of us who are from "everything must be private by default" school of thought.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [December 15, 2021, 10:33pm UTC](https://forums.swift.org/t/pitch-new-access-control-for-access-inside-type-from-other-files/54051/7 "2021-12-15T22:33:17Z")

</div>

I'd also encourage one to look at the past threads. Maybe also summarize what was debated and see if they have new things to put on the table. It tends to get eyes rolling when the discussion starts rehashing old stuff.

---

<div class="post-metadata">

**Author:** ![arsen](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@arsen](https://forums.swift.org/u/arsen)\
**Post date:** [January 25, 2022, 3:48am UTC](https://forums.swift.org/t/pitch-new-access-control-for-access-inside-type-from-other-files/54051/8 "2022-01-25T03:48:26Z")

</div>

Cool suggestion 👍👍👍

I think the new keyword name can be extentionprivate like fileprivate 😁
