# How to define a generic type's Access Level?

**URL:** <https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025>\
**Category:** Using Swift\
**Tags:** access-control\
**Created:** [June 27, 2018, 10:48am UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025 "2018-06-27T10:48:14Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![cuikun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cuikun/32/2900_2.png) [@cuikun](https://forums.swift.org/u/cuikun)\
**Post date:** [June 27, 2018, 10:48am UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/1 "2018-06-27T10:48:14Z")

</div>

```swift
fileprivate class someGenerics<T> where T: Equatable {
    private var type:T?
    func get(type: T) {
    }
    
    func someFunc<U>(unit: U) {
        
    }
}

```

```swift
private T // error

```

---

<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:** [June 27, 2018, 11:20am UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/2 "2018-06-27T11:20:56Z")

</div>

If you mean the access level of a generic parameter, `T`, that is not possible. But it is also senseless, since generic parameters in universal types (generic types) are supposed to be provided by the consumer upon instantiation. Could you share what you're trying to achieve by doing so, so we can help you?

If you want some members of the class to be generic, but not the class itself, use generics on those members, i.e.

```swift
class Foo {
  func get<T>(type: T) where T: Equatable {...}
}

```

---

<div class="post-metadata">

**Author:** ![cuikun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cuikun/32/2900_2.png) [@cuikun](https://forums.swift.org/u/cuikun)\
**Post date:** [June 27, 2018, 11:25am UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/3 "2018-06-27T11:25:21Z")

</div>

I don't understand the description of access control of generic in the language guide.could you write a demo for below?

> Generics  
> The access level for a generic type or generic function is the minimum of the access level of the generic type or function itself and the access level of any type constraints on its type parameters.

---

<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:** [June 27, 2018, 11:29am UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/4 "2018-06-27T11:29:50Z")

</div>

It means that if your, let's say, function is less accessible than the types used in its generic requirements, the access level of the function persists. Example:

```swift
public protocol P {...}

fileprivate func foo<T: P>(_ arg: T) {...}

// foo is obviously fileprivate despite P being public

```

The opposite case, when a type used in a generic requirement is less accessible, is simply illegal.

---

<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:** [June 27, 2018, 11:50am UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/5 "2018-06-27T11:50:54Z")

</div>

The documentation isn't clear enough about when this is true in terms of the language rules. But formally, although illegal in Swift, the statement is also correct in the case when a type used in a generic requirement is less accessible than the declaration. Naturally, you won't be able to use the declaration beyond the scope of that type's access level anyway.

Apologies for the editing, I realized it's more understandable this way.

---

<div class="post-metadata">

**Author:** ![genghiscon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/genghiscon/32/12335_2.png) [@genghiscon](https://forums.swift.org/u/genghiscon)\
**Post date:** [July 10, 2020, 7:22pm UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/6 "2020-07-10T19:22:09Z")

</div>

The docs still read the following as of WWDC 2020 and Swift 5.2:

> “The access level for a generic type or generic function is the minimum of the access level of the generic type or function itself and the access level of any type constraints on its type parameters.”
> 
> Excerpt From: Apple Inc. “The Swift Programming Language (Swift 5.2).” Apple Books. [‎The Swift Programming Language (Swift 5.7) on Apple Books](https://books.apple.com/us/book/the-swift-programming-language-swift-5-2/id881256329)

A quick example shows some of the weirdness that isn't fully captured in this statement:

```
private protocol myPrivateProtocol {
  func foo() -> Void
}

fileprivate func doTheGenericDance<T: myPrivateProtocol>(withFoo: T) -> Void {
  withFoo.foo()
}

```

The above compiled successfully: A case where the generic requirement is less accessible than the function's access.

So, it appears that the opposite case is not always illegal. It appears not, as there is a special exception for the relationship between private / file-private.

---

<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:** [July 10, 2020, 7:31pm UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/7 "2020-07-10T19:31:48Z")

</div>

`private` at file level is equivalent to `fileprivate`.

---

<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:** [July 10, 2020, 7:32pm UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/8 "2020-07-10T19:32:08Z")

</div>

~~At the file level, `private` and `fileprivate` are synonyms.~~ EDIT: @Jon_Shier beat me to it 🙂

---

<div class="post-metadata">

**Author:** ![genghiscon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/genghiscon/32/12335_2.png) [@genghiscon](https://forums.swift.org/u/genghiscon)\
**Post date:** [July 10, 2020, 7:32pm UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/9 "2020-07-10T19:32:28Z")

</div>

> `private` at file level is equivalent to `fileprivate` .

Well, now that's useful knowledge! Thanks!

---

<div class="post-metadata">

**Author:** ![genghiscon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/genghiscon/32/12335_2.png) [@genghiscon](https://forums.swift.org/u/genghiscon)\
**Post date:** [July 10, 2020, 8:15pm UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/10 "2020-07-10T20:15:49Z")

</div>

@Jon_Shier is there a mention in The Swift Programming Language of this fact? It seems useful and should probably be there, but I didn't come across it in the Access Control section.

---

<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:** [July 10, 2020, 8:40pm UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/11 "2020-07-10T20:40:38Z")

</div>

Not that I can see, I haven't read that book in quite a while. You can request it be added by filing a bug with Apple.

---

<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:** [July 10, 2020, 8:43pm UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/12 "2020-07-10T20:43:29Z")

</div>

Actually, TSPL calls out an important way in which they are not _quite_ the same in [this section](https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html#ID11):

> The access control level of a type also affects the default access level of that type’s _members_ (its properties, methods, initializers, and subscripts). If you define a type’s access level as private or file private, the default access level of its members will also be private or file private [respectively].

---

<div class="post-metadata">

**Author:** ![genghiscon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/genghiscon/32/12335_2.png) [@genghiscon](https://forums.swift.org/u/genghiscon)\
**Post date:** [July 10, 2020, 9:31pm UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/13 "2020-07-10T21:31:05Z")

</div>

@Jumhyn based on this reading, are you saying the the equivalency of `private` to `fileprivate` wouldn’t necessarily hold true within a file for members of a type?

---

<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:** [July 10, 2020, 9:44pm UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/14 "2020-07-10T21:44:26Z")

</div>

When you're dealing with explicit access control, `private` and `fileprivate` aren't equivalent in general:

```swift
struct A {
  private var x = 0
  fileprivate var y = 0
}

// Same file...
print(A().x) // Error
print(A().y) // Ok

```

After actually testing, though, it doesn't appear that the language behavior agrees with the excerpt I posted from TSPL:

```swift
private struct A {
  var x = 0
  private var y = 0
}

fileprivate struct B {
  var x = 0
}

print(A().x) // Ok! (I.e., x is not actually private)
print(A().y) // Error
print(B().x)

```

EDIT: Reported as [SR-13196](https://bugs.swift.org/browse/SR-13196).

---

<div class="post-metadata">

**Author:** ![genghiscon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/genghiscon/32/12335_2.png) [@genghiscon](https://forums.swift.org/u/genghiscon)\
**Post date:** [July 10, 2020, 10:09pm UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/15 "2020-07-10T22:09:52Z")

</div>

Would a simple solution to this be a built-in `accessControlLevel(of:)` function, similar to `type(of:)`?

---

<div class="post-metadata">

**Author:** ![genghiscon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/genghiscon/32/12335_2.png) [@genghiscon](https://forums.swift.org/u/genghiscon)\
**Post date:** [July 10, 2020, 11:08pm UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/16 "2020-07-10T23:08:02Z")

</div>

@Jon_Shier @Jumhyn Would it be more accurate to say that at the global level of a file, `fileprivate` and `private` are equivalent, but not in the general case? That seems more accurate to me.

---

<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:** [July 10, 2020, 11:10pm UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/17 "2020-07-10T23:10:20Z")

</div>

"file level" and "global level of a file" are the same thing.

---

<div class="post-metadata">

**Author:** ![genghiscon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/genghiscon/32/12335_2.png) [@genghiscon](https://forums.swift.org/u/genghiscon)\
**Post date:** [July 10, 2020, 11:11pm UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/18 "2020-07-10T23:11:46Z")

</div>

Ah - Yes, that does make sense 😅 Thanks again!

---

<div class="post-metadata">

**Author:** ![phoneyDev](https://avatars.discourse-cdn.com/v4/letter/p/49beb7/32.png) [@phoneyDev](https://forums.swift.org/u/phoneyDev)\
**Post date:** [July 11, 2020, 4:32pm UTC](https://forums.swift.org/t/how-to-define-a-generic-types-access-level/14025/19 "2020-07-11T16:32:28Z")

</div>

File scope variables are available anywhere in the file. `private` variables are available in their scopes only. `private let` and `private var` variables are available anywhere in the file only.
