title: What's the preferred way to encode a "nullable" field in protobuf 2?
body: <p>I am defining a ProtoBuf message where I want to have a "nullable" field -- i.e., I want to distinguish between the field having a value and not having a value.  As a concrete example, let's say I have "x" and "y" fields to record the coordinates of some object.  But in some cases, the coordinates are not known.  The following definition will <strong>not</strong> work, because if x or y are unspecified, then they default to zero (which is a valid value):</p>
  
  <pre><code>message MyObject {
      optional float x = 1;
      optional float y = 2;
  }
  </code></pre>
  
  <p>One option would be to add a boolean field recording whether the corresponding field's value is known or not.  I.e.:</p>
  
  <pre><code>message MyObject {
      optional bool has_x = 1; // if false, then x is unknown.
      optional bool has_y = 2; // if false, then y is unknown.
      optional float x = 3; // should only be set if has_x==true.
      optional float y = 4; // should only be set if has_y==true.
  }
  </code></pre>
  
  <p>But this imposes some extra book-keeping -- e.g., when I set the x field's value, I must always remember to also set has_x.  Another option would be to use a list value, with the convention that the list always has either length 0 or length 1:</p>
  
  <pre><code>message MyObject {
      repeated float x = 1; // should be empty or have exactly 1 element.
      repeated float y = 2; // should be empty or have exactly 1 element.
  }
  </code></pre>
  
  <p>But in this case, the definition seems a bit misleading, and the interface isn't much better.</p>
  
  <p>Is there a third option that I haven't thought of that's better than these two?  How have you dealt with storing nullable fields in protobuf?</p>