# Make it easier to share state

**URL:** <https://forums.swift.org/t/make-it-easier-to-share-state/58471>\
**Category:** Pitches\
**Tags:** program-architecture\
**Created:** [June 25, 2022, 12:19pm UTC](https://forums.swift.org/t/make-it-easier-to-share-state/58471 "2022-06-25T12:19:55Z")\
**Posts on this page:** 4\
**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:** [June 25, 2022, 12:19pm UTC](https://forums.swift.org/t/make-it-easier-to-share-state/58471/1 "2022-06-25T12:19:55Z")

</div>

Sometimes I wish that there were a magical language construct that makes it easier to share some state under a common roof.

Even the **module** construct in latest _C++_ does not seem to provide this feature.

Although **enum** , **struct** and **class** types in _Swift_ allow the declaration of nested types, they don't make it easier to have some state shared among instances of those nested types. Consequently, one has to resort to using singletons or passing references around to make state sharing possible.

To make this possible, we could introduce a new type (more like a class) and name it **domain** , **module** or **world** , which can be used to create a compound type that provides a scope for declaring both shared state and nested types which use that state.

For example, using **world** as the new type, we can declare a _Simulation_ **world** that has both properties for shared state and nested types using that state.

```swift
world Simulation {
   // Type for shared state, doesn't have to be here - can be declared anywhere

   struct Parameters {
      ...
   }

   // All properties constitute shared state
   let parameters : Parameters
   static var instanceCounter : Int = 0

   // Initialisation
   init (parameters : Parameters) {
      self.parameters = parameters
      instanceCounter += 1
   }

  // Instance creation function for state-sharing types
  func create <T> (...) -> T where T == BarEngine or T == FooEngine {
     return T (...)
  }

  // State-sharing types must opt in
 @StateSharing class BarEngine {
     // All instances of this have access to shared state
     ...
  }

  @StateSharing class FooEngine {
     // All instances of this have access to shared state
     ...
  }

  // These don't share state
  class BarHelper {
     ...
  }

  struct FooHelper {
     ...
  }

  // Etc
}

```

Then, we can run multiple simulation engines in parallel in different instances of the _Simulation_ world:

```swift
// Create a simulation world
// Create shared state - not shown
let s1 = Simulation (...)

// Engines in this world
let engine11 = s1.create <.BarEngine> ()
let engine12 = s1.create <.FooEngine> ()

// Run the engines
Task {
   engine11.run ()
}

Task {
   engine12.run ()
}

// Create a second world
// Create shared state - not shown
let s2 = Simulation (...)

// Engines in second world
let engine21 = s2.create <.BarEngine> ()
let engine22 = s2.create <.FooEngine> ()

// Run the engines
Task {
   engine21.run ()
}

Task {
   engine22.run ()
}

// Wait for results
...

```

Everyone is invited to brainstorm this magical new construct 🙂

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [June 25, 2022, 2:16pm UTC](https://forums.swift.org/t/make-it-easier-to-share-state/58471/2 "2022-06-25T14:16:22Z")

</div>

This is kind of swimming against the flow. Reducing shared mutable state has been a huge movement in programming language design in the past 20 years.

How would this "world-owned" shared state be tested?

---

<div class="post-metadata">

**Author:** ![Sajjon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sajjon/32/12044_2.png) [@Sajjon](https://forums.swift.org/u/Sajjon)\
**Post date:** [June 25, 2022, 3:44pm UTC](https://forums.swift.org/t/make-it-easier-to-share-state/58471/3 "2022-06-25T15:44:06Z")

</div>

Is it dependency injection you find tedious? There are tools for that, such as Swinject.

---

<div class="post-metadata">

**Author:** ![technogen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technogen/32/45489_2.png) [@technogen](https://forums.swift.org/u/technogen)\
**Post date:** [June 25, 2022, 7:10pm UTC](https://forums.swift.org/t/make-it-easier-to-share-state/58471/4 "2022-06-25T19:10:16Z")

</div>

An approach similar to SwiftUI's Environment looks far superior to me than Swinject. In fact, I was so impressed by it, that I reverse-engineered it. The storage part of it I implemented as a standalone library [AttributeKit](https://github.com/gor-gyolchanyan-swift/attribute-kit).
