Skip to content

Conversation

@dbaileychess
Copy link
Collaborator

C# implementation of Optional Scalars using Nullable value types (Nullable or T?).

Modified the FlatBufferBuilder library to add methods that handle the Scalar Nullables, and had the code gen use these new methods. I toyed with the idea of doing the same logic in the generated code, but thought I could better test by moving that logic to the library.

This does auto-box the values, so we might want to do what was done in #6212 with emitting hasMaybe() methods.

Much credit to @paulovap for his Java implementation and tests (#6212).

I had a general question about optional scalars, what happens if you write a value to the field, but later want to set it to 'null'. I didn't see a good way to reset the vtable entry to = 0, do we need to add methods to do this in all languages? @aardappel @CasperN

@dbaileychess dbaileychess requested review from CasperN and aardappel and removed request for CasperN October 28, 2020 06:05
@dbaileychess
Copy link
Collaborator Author

Ran test/generate_code.sh to show no changes to normal schema.
Ran . src/clang-format-git.sh which caught some additional formatting issues that I left in.

@dbaileychess
Copy link
Collaborator Author

Implements #6014 in C#.

@CasperN
Copy link
Collaborator

CasperN commented Oct 28, 2020

I had a general question about optional scalars, what happens if you write a value to the field, but later want to set it to 'null'. I didn't see a good way to reset the vtable entry to = 0, do we need to add methods to do this in all languages?

We haven't discussed what to do about mutation w.r.t optional scalars. I agree, we should have a way to set the vtable entry to 0 in all languages. This probably would be a one way operation that "leaks memory" inside the buffer, unless the mutation API remembers where the scalar used to be.

Copy link
Collaborator

@aardappel aardappel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this fits C# rather nicely!

I don't think we need to be worried about auto-boxing, since a scalar nullable in C# is equivalent to a struct I presume (value type) whereas the problem in Java was that it became a dynamic memory allocation.

As for mutation: the existing mutation API already doesn't support setting a value that is not present (and it returns false). While the reverse is in theory slightly easier the problem of shared vtables persists, so for consistency I would suggest that this is simply not possible.

@dbaileychess
Copy link
Collaborator Author

@CasperN Didnt' know if you wanted to look over things since this is your feature, if not I can submit.

@CasperN
Copy link
Collaborator

CasperN commented Oct 30, 2020

Looks good to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants