# Law of Exclusivity: Violation or not with class type

**URL:** <https://forums.swift.org/t/law-of-exclusivity-violation-or-not-with-class-type/23645>\
**Category:** Using Swift\
**Tags:** memory-safety\
**Created:** [April 24, 2019, 10:54pm UTC](https://forums.swift.org/t/law-of-exclusivity-violation-or-not-with-class-type/23645 "2019-04-24T22:54:09Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Andrew\_Trick](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew_trick/32/323_2.png) [@Andrew\_Trick](https://forums.swift.org/u/Andrew_Trick)\
**Post date:** [April 25, 2019, 12:28am UTC](https://forums.swift.org/t/law-of-exclusivity-violation-or-not-with-class-type/23645/6 "2019-04-25T00:28:00Z")

</div>

For those who are interested, this is diagnosed at run time because the closure here names a global variable rather than capturing a local variable.

```swift
var u = User()
u.setName("Kanga") { u.name = "haha" }

```

It's an unfortunate distinction between top-level code and code within a function body. To diagnose this statically, the compiler would need to analyze all closure bodies even though they don't mutably capture anything. It's possible to do this, but I'd much rather just not have top-level variable declarations treated like globals--that will always be a source of confusion where the compiler behavior is significantly different.

---

_[View the full topic](https://forums.swift.org/t/law-of-exclusivity-violation-or-not-with-class-type/23645)._
