Draft using records for data types in FileFormats.W3d #809
+37
−70
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.
I was looking at enabling nullable reference types in OpenSage.FileFormats.W3d, and found a lot of these immutable data types. I wanted to propose moving these over to records. Records are purpose-built immutable reference types with a clear contract and I think this would be a good use case for them.
I've only migrated a few types (including the core
W3dChunk
) in this draft as a sample, as there are a lot of types to migrate so I don't want to go all-in without first making sure it's a direction we want to go in. At a glance it definitely looks possible. As only a few types have been migrated and one of them is a base type for many things, this will not compile as-is.One downside I could see is that large migrations like this can muddy up the commit history. Also the phrase "if it ain't broke, don't fix it" comes to mind. This is really just one way of getting to nullable reference types in this project. The other might look something like this (note the
null!
assignment):What I don't like about this method is that it relies on the developer to verify
Prop1
is actually set, which to me defeats the purpose.Another alternative:
But now we're just getting closer to record types and at that point, I figure we just lean into them?