Why can’t we have these kinds of globals?

Why can’t I declare a type-level property for a generic nominal (value?) type? Why can’t we define a static variable inside a (generic?) function?

Vide:

Swift has never supported static variables in functions. I believe this was a deliberate design choice, but I have not seen any discussion about it.

Regardless, for non-generic functions, you can easily achieve the same effect with a static variable on a type declared within the function:

func foo() {
  struct Foo { static var x = 0 }
  Foo.x += 1
  print(Foo.x)
}
1 Like

Probably because they have an extremely limited number of uses. And I'd say most of those uses are better served by alternative designs.

1 Like