title: Why does F# generic struct have extra __dummy field?
body: <p>Using F# Interactive, you can verify the following sizes:</p>
  
  <pre><code>// sizeof&lt;A&gt; = 4 bytes
  type A (i: int) = struct end
  
  // sizeof&lt;B&lt;int&gt;&gt; = 8 bytes (use any type parameter)
  type B&lt;'T&gt; (i: int) = struct end
  </code></pre>
  
  <p>The reason for the extra size seems to be the presence of an integer <code>__dummy</code> field in the generic case. Using F# Interactive again, you can see this using <code>typeof</code>:</p>
  
  <ul>
  <li><code>typeof&lt;A&gt;</code> shows <code>DeclaredFields = [|Int32 i|]</code></li>
  <li><code>typeof&lt;B&lt;int&gt;&gt;</code> shows <code>DeclaredFields = [|Int32 i; Int32 __dummy|]</code></li>
  </ul>
  
  <p>I don't understand why this <code>__dummy</code> field has been added.</p>
  
  <p>I think the code responsible for adding it is here:</p>
  
  <p><a href="https://github.com/fsharp/FSharp.Compiler.Service/blob/master/src/fsharp/ilxgen.fs">https://github.com/fsharp/FSharp.Compiler.Service/blob/master/src/fsharp/ilxgen.fs</a></p>
  
  <p><a href="https://github.com/fsharp/FSharp.Compiler.Service/blob/master/src/fsharp/ilxgen.fs#L6377">Line 6377</a> shows this:</p>
  
  <pre><code>if requiresExtraField then 
      yield mkILInstanceField("__dummy",cenv.g.ilg.typ_int32,None,ILMemberAccess.Assembly) ]
  </code></pre>
  
  <p><a href="https://github.com/fsharp/FSharp.Compiler.Service/blob/master/src/fsharp/ilxgen.fs#L6290">Line 6290</a> is where <code>requiresExtraField</code> is defined: </p>
  
  <pre><code>let requiresExtraField = 
      let isEmptyStruct = 
          (match ilTypeDefKind with ILTypeDefKind.ValueType -&gt; true | _ -&gt; false) &amp;&amp;
          // All structs are sequential by default 
          // Structs with no instance fields get size 1, pack 0
          tycon.AllFieldsAsList |&gt; List.exists (fun f -&gt; not f.IsStatic)
  
      isEmptyStruct &amp;&amp; cenv.opts.workAroundReflectionEmitBugs &amp;&amp; not tycon.TyparsNoRange.IsEmpty
  </code></pre>
  
  <p>I assume that <code>isEmptyStruct</code> is supposed to mean that the struct does not have any instance fields. But the code as written is testing whether the struct <em>does</em> have any instance fields, which for most structs, including mine, is going to be true. I think the last part of the final test is whether there are any generic type parameters. So <code>requiresExtraField</code> is <code>false</code> for <code>type A</code> (not generic) and <code>true</code> for <code>type B</code> (generic type).</p>
  
  <p>Is this a compiler bug, or is the code correct? If it is correct, then what's the purpose of this <code>__dummy</code> field? Is there some way I can avoid having it?</p>
  
  <p>As another test, I removed my one and only instance field, and not surprisingly, I got the following sizes, showing that the <code>__dummy</code> field was no longer added:</p>
  
  <pre><code>// sizeof&lt;AA&gt; = 1
  type AA = struct end
  
  // sizeof&lt;BB&lt;int&gt;&gt; = 1
  type BB&lt;'T&gt; = struct end
  </code></pre>
  
  <p>The reason I want to have a value type, rather than a reference type, is that I will be storing lots of these objects in my data structures, not just passing them around.</p>