title: dlang inheritance design for types passed between threads
body: <p>I'm writing a multithreaded program in the D programming language, but am pretty new to the language. There is a restriction on types passed between threads using the <code>Tid.send()</code> and <code>receive[Only]()</code> APIs in the <code>std.concurrency</code> package that they must be value types or must be constant to avoid race conditions between the sender and receiver threads. I have a simple <code>struct Message</code> type that I have been passing by value:</p>
  
  <pre><code>enum MessageType {
      PrepareRequest,
      PrepareResponse,
      AcceptRequest,
      Accepted
  }
  
  struct Message {
      MessageType type;
      SysTime timestamp;
      uint node;
      ulong value;
  }
  </code></pre>
  
  <p>However, some <code>MessageType</code>s don't have all the fields, and it's annoying to use a switch statement and remember which types have which fields when I could use polymorphism to do this work automatically. Is using an <code>immutable</code> class hierarchy recommended here, or is the approach I'm already using the best way to go, and why?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Also, if I should use immutable classes, what's the recommended way to create immutable objects of a user-defined class? A static method on the class they come from that casts the return value to <code>immutable</code>?</p>