# Allowing type-level properties in generic types

**URL:** <https://forums.swift.org/t/allowing-type-level-properties-in-generic-types/32487>\
**Category:** Discussion\
**Tags:** properties, static\
**Created:** [January 12, 2020, 1:25pm UTC](https://forums.swift.org/t/allowing-type-level-properties-in-generic-types/32487 "2020-01-12T13:25:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [January 12, 2020, 1:25pm UTC](https://forums.swift.org/t/allowing-type-level-properties-in-generic-types/32487/1 "2020-01-12T13:25:08Z")

</div>

Continuing the discussion from [Nested struct inside another struct, not the same as standalone struct?](https://forums.swift.org/t/nested-struct-inside-another-struct-not-the-same-as-standalone-struct/32472/4):

> [@Nested struct inside another struct, not the same as standalone struct?](https://forums.swift.org/t/nested-struct-inside-another-struct-not-the-same-as-standalone-struct/32472/4):
>
> You cannot declare static properties inside generic types. The language doesn't support it today. This is because the semantics aren't very clear, should `Structure<Int>` share the same static variables as `Structure<String>` or should they be distinct? You _probably_ should be able to do the former, but that hasn't been decided yet and behaviour and syntax are simply not designed. Hence, the compiler bans it outright.

What if we use:

```
struct MyType<T> {
    super static allGlobal: Int // A single object shared with all instantiated types
    static perVersionGlobal: T // Separate versions per type instantiated
}

```

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [January 12, 2020, 1:36pm UTC](https://forums.swift.org/t/allowing-type-level-properties-in-generic-types/32487/2 "2020-01-12T13:36:10Z")

</div>

> [@CTMacUser](#):
>
> perVersionGlobal

Seems like a perverse name to me. How about `perTypeGlobal`?

---

<div class="post-metadata">

**Author:** ![tangent-vector](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tangent-vector/32/19179_2.png) [@tangent-vector](https://forums.swift.org/u/tangent-vector)\
**Post date:** [January 12, 2020, 3:59pm UTC](https://forums.swift.org/t/allowing-type-level-properties-in-generic-types/32487/3 "2020-01-12T15:59:30Z")

</div>

I don’t think the blocking issue here is about syntax.

The per-type/-instantiation case is the more obvious/correct semantic, but (I suspect) isn’t trivial to implement.

The “super static” case has the thorny semantic problem that the compiler must ensure it doesn’t reference any of the generic parameters directly or indirectly. When you consider that this is in scope where associated types might be getting inferred, it isn’t always going to be clear (to the programmer) which in-scope types do or do not depend on the generic parameters.

If you want the behavior of the proposed `super static` then a global-scope property would do the job without any thorny semantic questions or new syntax.
