0

Based on a very good approach for null fields proposed by the main contributor to flatbuffers:

https://github.com/google/flatbuffers/issues/333#issuecomment-155856289

The easiest way to get a null default for an integer field is to wrap it in a struct. This will get you null if scalar isn't present. It also doesn't take up any more space on the wire than a regular int.

struct myint { x:int; }
table mytable { scalar:myint; }enter code here

this will get you null if scalar isn't present. It also doesn't take up any more space on the wire than a regular int.

Also based on the flatbuffers documentation:

https://google.github.io/flatbuffers/md__schemas.html

You can't change types of fields once they're used, with the exception of same-size data where a reinterpret_cast would give you a desirable result, e.g. you could change a uint to an int if no values in current data use the high bit yet.

My question is can I treat int as reinterpret_cast-able to myint?

In other words, if I start with just a simple int as a field, can I later on decide that I actually want this int to be nullable and change it to myint? I know that all values that used to be default value in the first int schema will be read as null in the myint schema and I am ok with that.

Of course the obvious follow up question is can I do the same thing for all scalar types?

Vassil Lunchev
  • 159
  • 1
  • 10

1 Answers1

1

Though that isn't explicitly documented, yes, int and myint are wire-format compatible (they are both stored inline). Like you say, you will lose any default value instances to become null.

Aardappel
  • 5,559
  • 1
  • 19
  • 22
  • My question is exactly provoked by the fact that this is not explicitly documented. If you were to speculate for the future, do you think that this wire-format compatibility will most likely stay compatible or your speculation would be that most likely it will become incompatible at some point? – Vassil Lunchev Apr 16 '17 at 08:37
  • 1
    I can't think of a reason why it would ever become incompatible. Many things about the FlatBuffers wire format are set in stone at this point, and the fact that structs are memory-equivalent to the scalars they hold is not going to change. – Aardappel Apr 16 '17 at 16:06