Write-through for tables #209
-
Why is write-through only supported for structs and not tables? Is this something that would be considered for a future release? Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
It's something that I have thought about. The short version is: write through for tables has some weird edge cases that structs don't, which can lead to surprising and nonobvious outcomes. You probably know this, but the primary difference between structs and tables is that all struct fields are mandatory and are always written, while table fields (even primitives) are skipped if they match their default value. There is no technical reason that it won't work. However, one of the things I really think about when adding something is: "Would this surprise me if I didn't know how this worked under the hood?" and "Can I imagine a way to use this in which I might be surprised?" Here's an example of something I consider surprising:
FancyTable three = new() { Value = 3 };
FancyTable zero = new() { Value = 0 };
byte[] serializedThree = FlatBufferSerializer.Default.Serialize<FancyTable>(three);
FancyTable parsedThree = FlatBufferSerializer.Parse<FancyTable>(serializedThree);
byte[] serializedZero = FlatBufferSerializer.Default.Serialize<FancyTable>(zero);
FancyTable parsedZero = FlatBufferSerializer.Parse<FancyTable>(serializedZero);
// This works, because 3 is not the default value of 0, so there is space in the buffer for it to be written.
parsedThree.Value = 10;
// This fails because FlatBuffers implemenations simply skip a table field when it matches its default value.
// So there's no space for the value to go. FlatSharp would be forced to throw an exception here.
parsedZero.Value = 10; Since FlatSharp does everything with property overrides and inheritance, there's no good mechanism I have to add "TryWriteThrough" methods, since the only thing you as a developer see is the base class, and the subclass is the one doing all the interesting work. The way I've personally worked with this is to just define some simple wrapper structs:
The serialized output of this is identical to the above, though in this case if the Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
FlatSharp 6.0 (upcoming release) will support write through in some additional scenarios:
|
Beta Was this translation helpful? Give feedback.
It's something that I have thought about. The short version is: write through for tables has some weird edge cases that structs don't, which can lead to surprising and nonobvious outcomes. You probably know this, but the primary difference between structs and tables is that all struct fields are mandatory and are always written, while table fields (even primitives) are skipped if they match their default value.
There is no technical reason that it won't work. However, one of the things I really think about when adding something is: "Would this surprise me if I didn't know how this worked under the hood?" and "Can I imagine a way to use this in which I might be surprised?"
Here's an exampl…