title: Binary serialization of mutable F# record
body: <p>I have used binary serialization to save an F# record from a C# class. All works fine:</p>
  
  <p>F#:</p>
  
  <pre class="lang-ml prettyprint-override"><code>type GameState =
      {
          LevelStatus : LevelStatus
          Grid : Variable&lt;Option&lt;Ball&gt; [,]&gt;
          ...
      }
  let game_state : GameState = aGameState()
  </code></pre>
  
  <p>C#:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public void OnSaveGame() {
      using (var stream = File.Open("game_status.sav", FileMode.Create))
      {
          var binary_formatter = new BinaryFormatter();
          binary_formatter.Serialize(stream, PuzzleBobble.game_state);
      }
  }
  </code></pre>
  
  <p>Now, I'm refactoring my F# module and I would like to have a mutable record to serialize:</p>
  
  <pre class="lang-ml prettyprint-override"><code>let mutable game_state = aGameState()
  game_state &lt;- myGameState()
  </code></pre>
  
  <p>This way the file is created but when I try to deserialize it I get a null object.<br>
  I've changed nothing of my previous implementation, except the added mutable keyword.</p>
  
  <p>My question is: is there anything wrong with serialization of a mutable F# record? Or serialization it self is right, and I have to look for a different error somewhere else in my code?</p>
  
  <p>EDIT:</p>
  
  <p>Even accessing the record through methods like suggested @Brian, it seems not to work.</p>
  
  <p>Here's some more details. When I deserialize the previous saved object this way (that works without game_state declared mutable): </p>
  
  <pre><code>public void OnLoadGame() {
          using (var stream = File.Open("game_status.sav", FileMode.Open))
          {
              var binary_formatter = new BinaryFormatter();
              try
              {
                  GameLogic.GameState state = binary_formatter.Deserialize(stream) as GameLogic.GameState;
                  GameLogic.load_game_state(state);
              }
              catch (ArgumentNullException e) {
                  Console.WriteLine(e.Message);  
              }       
  
  
          }
      }
  </code></pre>
  
  <p>I get the following exception:</p>
  
  <blockquote>
    <p>'System.ArgumentNullException' in FSharp.Core.dll</p>
  </blockquote>