F#: Fixing code generation for enum style discriminated unions and for discriminated unions with 4 of more cases #9095
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes code generation for the two remaining "flavors" of discriminated unions (DU) which currently do
not work. The two remaining "flavors" are enum style discriminated unions, and discriminated unions with 4 or more
cases. The key takeaway is that both these "flavors" of DUs have an
int _tag
backing field for theTag
propertywhich we must serialize/deserialize. For other types of DUs the
Tag
property is not backed by a backing field,and we only need to serialize/deserialize the
item{int}
.Enum style discriminated unions
Enum style DUs are not based on inheritance, and they do not have an
Item{int}
property, only aTag
property.This tag must be used when serializing/deserializing.
Discriminated unions with 4 or more cases
When a fourth case is introduced in the DU, the abstract base class which all the cases inherit from gets a
_tag
backing field for the
Tag
property. This must be serialized/deserialized as well, in addition to theItem{int}
in each of the concrete cases.
Switching to identifying the backing fields directly rather than properties
When identifying the data we need to serialize/deserialize in
FSharpUnionCaseTypeDescription.GetUnionCaseDataMembers
,rather than looking for the properties
Item{int}
andTag
we go looking directly for the backing fieldsitem{int}
and_tag
. Since single, double and triple case DUs do not have a backing field for theTag
property,and we do not have to serialize/deserialize the tag for these "flavors" anyway, it makes it easier to look for the backing
fields directly. That way, we don't have to treat the different "flavors" of DU separately. We serialize/deserialize
all
items{int}
, regardless of where they reside, and the_tag
, if it exists (enum style and 4 or more cases).Microsoft Reviewers: Open in CodeFlow
Fixes #8752
Fixes #8255