# What is a Function-local static in Swift?

**URL:** <https://forums.swift.org/t/what-is-a-function-local-static-in-swift/73470>\
**Category:** Using Swift\
**Tags:** static-variables\
**Created:** [July 26, 2024, 3:13am UTC](https://forums.swift.org/t/what-is-a-function-local-static-in-swift/73470 "2024-07-26T03:13:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [July 26, 2024, 3:13am UTC](https://forums.swift.org/t/what-is-a-function-local-static-in-swift/73470/1 "2024-07-26T03:13:59Z")

</div>

@Douglas_Gregor mentions **Function-local statics** in [Swift for C++ Practitioners, Part 8: Global Variables](https://www.douggregor.net/posts/swift-for-cxx-practitioners-global-variables/).

> ## Wrap-up
> 
> Global and static variables in Swift are conceptually similar to those in C++, and used for much the same purpose. However, Swift takes the "Initialize on First Use" principle all the way, with all global and static variables being initialized on first use. **Function-local statics** in Swift work identically to those in C++, based on the same "Initialize on First Use" principle.

I thought they did not exist in Swift, but I tried it anyway:

```swift
func f () {
    static var u: Int = 7
    // error: Static properties may only be declared on a type
}

```

What are they?

---

<div class="post-metadata">

**Author:** ![Martin](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Martin](https://forums.swift.org/u/Martin)\
**Post date:** [July 26, 2024, 5:21am UTC](https://forums.swift.org/t/what-is-a-function-local-static-in-swift/73470/2 "2024-07-26T05:21:28Z")

</div>

I do not know for sure if that is what the article refers to, but you can define a function-local struct with a static property:

```swift
func f () {
    struct S {
        static var u: Int = 7
    }
}

```

See also:

> [@Why can't functions have static variables?](https://forums.swift.org/t/why-cant-functions-have-static-variables/12775):
>
> I'm sure this has been brought up before but as I couldn't find any previous discussion on it: The following is not allowed: func next() -\> Int { static var v = 0 // ERROR: Static properties may only be declared on a type v += 1 return v } But it is trivial to work around this: func next() -\> Int { struct S { static var v = 0 } S.v += 1 return S.v } print(next()) // 1 print(next()) // 2 print(next()) // 3 So what is the rationale behind not allowing static variables …

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Ignatyev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmitriy_ignatyev/32/11340_2.png) [@Dmitriy\_Ignatyev](https://forums.swift.org/u/Dmitriy_Ignatyev)\
**Post date:** [July 26, 2024, 6:37am UTC](https://forums.swift.org/t/what-is-a-function-local-static-in-swift/73470/3 "2024-07-26T06:37:37Z")

</div>

```swift
func f () {
    struct S {
        static var u: Int = 7
    }

  S.u += 1 // count function cals
}

```

I think this should be an error in Swift 6 due to data races check

---

<div class="post-metadata">

**Author:** ![Quedlinbug](https://avatars.discourse-cdn.com/v4/letter/q/77aa72/32.png) [@Quedlinbug](https://forums.swift.org/u/Quedlinbug)\
**Post date:** [July 26, 2024, 6:43am UTC](https://forums.swift.org/t/what-is-a-function-local-static-in-swift/73470/4 "2024-07-26T06:43:52Z")

</div>

`Static property 'u' is not concurrency-safe because it is non-isolated global shared mutable state`

Still usable if not variable, however.

```swift
func f() -> Int {
  enum FunctionLocalStatic {
    static let u: Int = {
      print("Prints 1 time, not 2")
      return 7
    } ()
  }

  return FunctionLocalStatic.u
}

f()
f()

```

---

<div class="post-metadata">

**Author:** ![crontab](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/crontab/32/7374_2.png) [@crontab](https://forums.swift.org/u/crontab)\
**Post date:** [July 26, 2024, 7:49am UTC](https://forums.swift.org/t/what-is-a-function-local-static-in-swift/73470/5 "2024-07-26T07:49:08Z")

</div>

> [@Dmitriy\_Ignatyev](#):
>
> I think this should be an error in Swift 6 due to data races check

Why would it be? A function-static variable is guaranteed to have the same isolation as the function, should be safe to use.

---

<div class="post-metadata">

**Author:** ![vns](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vns/32/37396_2.png) [@vns](https://forums.swift.org/u/vns)\
**Post date:** [July 26, 2024, 7:59am UTC](https://forums.swift.org/t/what-is-a-function-local-static-in-swift/73470/6 "2024-07-26T07:59:31Z")

</div>

Just `Task` is enough to create concurrent access to non-isolated property:

```swift
func f () {
    struct S {
        static var u: Int = 7
    }

    S.u += 1
    Task.detached {
        S.u -= 1
    }
}

```

---

<div class="post-metadata">

**Author:** ![crontab](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/crontab/32/7374_2.png) [@crontab](https://forums.swift.org/u/crontab)\
**Post date:** [July 26, 2024, 8:08am UTC](https://forums.swift.org/t/what-is-a-function-local-static-in-swift/73470/7 "2024-07-26T08:08:45Z")

</div>

> [@vns](#):
>
> ```swift
> Task.detached {
> S.u -= 1
> }
> 
> ```

That would trigger an error for a local variable and should do the same with a function-static one. I think it should be safe to treat func-statics the same way as locals in terms of concurrency safety.

---

<div class="post-metadata">

**Author:** ![j-f1](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/j-f1/32/12287_2.png) [@j-f1](https://forums.swift.org/u/j-f1)\
**Post date:** [July 26, 2024, 12:08pm UTC](https://forums.swift.org/t/what-is-a-function-local-static-in-swift/73470/8 "2024-07-26T12:08:58Z")

</div>

Another case to consider:

```swift
protocol P {
    static var u: Int { get set }
}

func f () -> any P.Type {
    struct S: P {
        static var u: Int = 7
    }

    return S.self
}

```
