-
Notifications
You must be signed in to change notification settings - Fork 480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Immutable constructor argument order issues result in NullReferenceException #188
Comments
You're right, in theory the order of properties/fields in a class is undefined (although in practice, it's processed line-by-line). We should not rely on the order as it's defined in the class. |
Agreed. BTW, this can get even more confusing when option class inheritance is in play. Spoiler: the inherited options (from the base options class) are expected at the end of the constructor parameter list. |
I'm opening up conversation about this issue because it interests me to find a solution. Throwing out a few ideas for consideration and discussion:
Is there any interest in supporting direct property setters to bypass the constructor? This might help reduce complication of option class inheritance. |
This defeats the point of immutable types (I suppose one could make the setters private but it would still not be truly immutable). |
The Exception, as described in #499, have been changed to:
|
@moh-hassan I would add some text like:
Otherwise a client may not understand why no matching constructor was found... |
I have had a report of at least one case where the properties returned by sealed class CommandLineArgs
{
public CommandLineArgs(string inputFilename, string? outputFilename)
{
InputFilename = inputFilename;
OutputFilename = outputFilename;
}
[Value(0, Required = true, Hidden = true)]
public string InputFilename { get; }
[Option('o', "out")]
public string? OutputFilename { get; }
}
|
As it currently stands, both the order and the names are required to match. If we simply remove the requirement for the order to match, then the bulk of the change in behaviour will be with code that is already broken. The Only case I can think of where code transitions from "working" to "broken" is if people define multiple parameters/properties/fields where the name only differs by case. Such code would seem problematic to me even without this change. If we are worried about this change, then perhaps add another property to |
This appears to be culture related. In particular, if I switch to I would consider this to be a fairly significant bug as it means that immutable argument objects can't be used in any application that might reasonably be used in a region like Turkey. |
For example:
Will result in:
The ideal solution IMO would be to find the right ctor by param names (rather than order). But even if that's not implemented, a better and more meaningful exception would be very helpful in debugging such issues.
The text was updated successfully, but these errors were encountered: