title: Swift compute field in struct on decode
body: <p>I'm working on an app which does a lot of real time computing based on values loaded in a struct using JSONDecoder(). I need to minimise computations in run time so I'd like to do some calculation up front. For this question I've created a working example with som fictional names for example purpose only</p>
  <p>This piece of code loads data from a json. My quest is to use data from one part and inject it in another part based on name similarity of these parts.</p>
  <pre><code>struct Structure: Decodable {
      
      static func withJSON(_ fileName: String) -&gt; Structure? {
          guard let url = Bundle.main.url(forResource: fileName, withExtension: &quot;json&quot;, subdirectory: &quot;Structures&quot;) else { return nil }
          guard let data = try? Data(contentsOf: url) else { return nil }
          do {
              let decoded = try JSONDecoder().decode(Structure.self, from: data)
              return decoded
          }
          catch{
              print(&quot;Unexpected error Structure withJSON: \(error).&quot;)
          }
          return nil
      }
      
      private enum Keys: CodingKey {
          case id
          case rooms
          case lists
      }
      
      let id: Int
      let rooms: [Room]
      let lists: [List]
      
      init(from decoder: Decoder) throws {
          
          let container = try decoder.container(keyedBy: Keys.self)
          id = try container.decode(Int.self, forKey: .id)
          rooms = try container.decode([Room].self, forKey: .rooms)
          lists = try container.decode([List].self, forKey: .lists)
      }
  }
  
  extension Structure {
      
      struct Room: Decodable {
          
          private enum RoomKeys: CodingKey {
              case roomName
              case dimension
              case doors
          }
          
          let roomName: String
          let dimension: [Double]
          let doors: Int
          
          init(from decoder: Decoder) throws {
              let container = try decoder.container(keyedBy: RoomKeys.self)
              roomName = try container.decode(String.self, forKey: .roomName)
              dimension = try container.decode([Double].self, forKey: .dimension)
              doors = try container.decode(Int.self, forKey: .doors)
          }
      }
  }
  
  extension Structure {
      
      struct List: Decodable {
          
          private enum RoomKeys: CodingKey {
              case roomName
              case calculated
          }
          
          let roomName: String
          var calculated: Double?
          
          init(from decoder: Decoder) throws {
              let container = try decoder.container(keyedBy: RoomKeys.self)
              roomName = try container.decode(String.self, forKey: .roomName)
              calculated = 0 // This variable should be filled with the array sum of Structure.Room.dimensions bonus for minus the doors of that (room * 2)
          }
      }
  }
  </code></pre>
  <p>In this over simplified example I'd like to compute Structure.Lists.calculated with the sum of the array which sits in rooms.dimension with both the SAME roomName. See this json example:</p>
  <pre><code>{
      &quot;id&quot;: 1,
      &quot;rooms&quot;: [
          {
              &quot;roomName&quot;: &quot;living&quot;,
              &quot;dimension&quot;: [6,4,9],
              &quot;doors&quot;: 2
          },
          {
              &quot;roomName&quot;: &quot;hall&quot;,
              &quot;dimension&quot;: [2,4,4],
              &quot;doors&quot;: 2
          },
          {
              &quot;roomName&quot;: &quot;bathroom&quot;,
              &quot;dimension&quot;: [1,1,1],
              &quot;doors&quot;: 2
          }
      ],
      &quot;lists&quot;:  [
          {
              &quot;roomName&quot;: &quot;living&quot;
          },
          {
              &quot;roomName&quot;: &quot;bathroom&quot;
          }
      ]
  }
  </code></pre>
  <p>In this example the optional Structure.List.calculated with the roomName &quot;living&quot; should be filled with the sum of dimension [6,4,9] thus 19 (bonus to subtract the doors * 2)</p>
  <p>Can this be done on the fly while decoding? Or is this data not available yet since the struct is still loading? Then what approach should I take?</p>