C#: Parameterless constructors for Variant structs#83207
C#: Parameterless constructors for Variant structs#83207Repiteo wants to merge 1 commit intogodotengine:masterfrom
Conversation
|
IINW this was discussed in #65051. |
|
Now I feel like a goober, I was looking for this in proposals but not in archived PRs I'm not entirely convinced that this is 1-1 with that though, because it didn't seem to present the idea of parameterless constructors in parity with |
|
The I feel like having parameterless constructors in structs is confusing for C# users. And my impression from the original discussion is that there was agreement on this.
The harm is that users may not realize that the constructor does something different to what they expect. I'd be OK with giving users the option, but that option already exists: the
That's not how compatibility works. Just because we discourage users from using an API, that doesn't mean we can then break that API. That said, I don't think we discourage users from using parameterless constructors, we just mention it in the documentation as one of the differences from GDScript, but if users need a zeroed struct they are free to use the parameterless constructor to get one. |
Maybe not in the main documentation, but several Identity docstrings certainly discourage it: I don't know if I'm completely sold on the concept being confusing, but I'm not gonna die on that hill. If we wanna let the behavior remain as-is, then that's totally fine by me So instead, let's flip this on its head: we can use a parameterless constructor to define As such, instead of relying on the Identity docstring to specify it should be used, which a user might miss, we can specify in the base constructor that using it will result in a zeroed-value result. It keeps old behavior 100% intact, while gaining the potential to specify for unaware users that the use-case might not be what they expect |
• Functionally identical to old behavior, but with docstrings to help guide potentially uninformed users
5d523a9 to
68ec192
Compare
Currently, it's documented that one of the differences between GDScript and C# is that certain struct types shouldn't use their constructors, instead opting for static readonly equivalents. This was because, at the time of writing, it was impossible for C# to define a parameterless constructor for a struct. However, starting with .NET6 (C#10), we now have that functionality!
Old implementation
This PR provides parity with GDScript by assigning parameterless constructors to the 6 problem types, setting their values to the GDScript equivalents. Theoretically, this shouldn't break compatability, as it's already suggested by the documentation to always avoid these constructors. However, in the case that a zeroed-value is what's desired, the docstrings mention the relevant C#10 specification where providing
defaultresults in exactly that. The one area where parity is arguable is fields, as their implicit value isdefaultrather thannew(); but in a C# context this is expected behavior (mirrors reference types and nullable value types being null, their default, implicitly)Input:
Output:
EDIT: Change of plans. The parameterless constructor will instead be used as a means of adding further documentation. It'll remain functionally identical to
default, but with an added note on how to emulate GDScript behavior if used as a constructor. Also removes the portion of documentation that explicitly discourages a plain constructor