It looks like now Unity just needs to update its compiler version and implement a DiagnosticSuppressor to suppress CS0649. A new version of Roslyn is probably not released yet since this was merged yesterday, but I thought I should let you guys know.
This was made very much with the problem Unity had in mind. Pinging @JoshPeterson , @joncham and @LeonhardP as they were the people responding in the original thread . Will you be implementing this feature in the engine?
I’m not quite sure how this works, but I assume that this won’t land in LTS - it requires updating the Roslyn version, which doesn’t really sound very LTS-y.
Can this thread get a reply from Unity. This is one of biggest annoyance I’ve ever dealt with - needing to “decorate” my code with dumb #pragma statements.
Until we get this, use the default keyword to explicitly initialize the field with a default value. The requested feature would allow us to skip this as well, but until then, it’s definitely better than pragma statements.
From the descriptions, I would say I want this. Would this be something that only Unity can add to the compiler or would it be possible for users to modify their local settings? If possible, we could start testing this.
We need to have a compiler which supports this feature.
The feature was merged into one of Roslyn’s feature branches on the 19th of June, and was merged from there into Roslyn’s master branch. The last Roslyn release was on the 21th of May. So unless Unity’s pulling Roslyn directly from Github instead of using official releases, it’ll be a while until this will be available in Unity.
Otherwise, using the default assignment causes some compilers in IDEs, like JetBrains Rider, to warn that a variable was redundantly assigned the default value.
I’m not sure if it can also be addressed with the Roslyn update, but I often see Visual Studio or ReSharper recommendations to convert simple Properties (that just return field values) to auto-properties even when the field has the [SerializeField] attribute.
[SerializeField]
private string descriptor;
public string Descriptor => this.descriptor;
That recommendation is desired when dealing with a regular field, however fields that have the [SerializeField] attribute must not be converted to an auto-property. Doing so would remove the field from the Inspector.
I would love to see this recommendation suppressed when the property is associated with a field marked with [SerializeField].
That’s not something that can be done on Unity’s side, it has to happen on Jetbrain’s side. It’s already an issue on their Github, so they’re aware of it.
Thing is, this works:
[field: Serializefield]
public string Descriptor { get; private set; }
Though the result gives a garbled look in the inspector. So it’s not completely wrong to convert that property, given that you’re on a Unity version that supports attributes targeting backing fields (C# 7+)