Allowing type-level properties in generic types

Continuing the discussion from Nested struct inside another struct, not the same as standalone struct?:

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
}

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

1 Like

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.

1 Like