Possible to have a WasDefaulted property added to C#? #2795
Replies: 14 comments
-
That extra bit of data would effectively be a private void MyMethod(int? intParameter = null)
{
if (intParameter == null) ...
} You won't be able to tell if someone explicitly passed in |
Beta Was this translation helpful? Give feedback.
-
Thanks for responding. Your suggestion is actually what I am using right now. I just wondered if a feature could be added to make it cleaner. The big drawback of this solution is that you can no longer specify an actual default value in the signature, so external documentation is required to inform callers of the defaults. Use case: The application needs to know which fields were actually supplied and which were defaulted so it can determine how confident its analysis is (based on how much real input exists). |
Beta Was this translation helpful? Give feedback.
-
I think to a great extent many of the use cases where I needed this could be solved if non constants were allowed as defaults. I believe there is a championed feature for this. |
Beta Was this translation helpful? Give feedback.
-
Yes, that would be a welcome addition too. |
Beta Was this translation helpful? Give feedback.
-
imo the new flag should belong to |
Beta Was this translation helpful? Give feedback.
-
Sounds like a good use case for overloading. If the value isn't provided, it calls a different method entirely. |
Beta Was this translation helpful? Give feedback.
-
In most cases, I'd agree, but some of the methods in our solution have many parameters. Providing an overload for every possible combination of missing parameters would be ridiculous. |
Beta Was this translation helpful? Give feedback.
-
Then sane defaults are really your only option. There's no other way to pass a "flag" into the method without breaking the signature of all methods that accept defaulted parameters. |
Beta Was this translation helpful? Give feedback.
-
Another option would be to define a struct to pass for the parameters which internally tracks a flag for each of the properties that are set: public struct MyMethodParameters {
private int foo;
private bool fooSet;
private string bar;
private bool barSet;
public int Foo { get => foo; set { foo = value; fooSet = true; } }
public bool IsFooSet => fooSet;
public string Bar { get => bar; set { bar = value; barSet = true; } }
public bool IsBarSet => barSet;
}
public void MyMethod(MyMethodParameters parameters) {
if (parameters.IsFooSet) {
int foo = parameters.Foo;
// use foo here
}
// etc.
}
MyMethod(new MyMethodParameters { Foo = 123 });
// or, with target typed new
MyMethod(new() { Foo = 123 }); |
Beta Was this translation helpful? Give feedback.
-
That allows me to know if a parameter was defaulted, but (like the other solution offered above) it doesn't allow me to specify default values. |
Beta Was this translation helpful? Give feedback.
-
You can return a default value from the property: public struct MyMethodParameters {
private static readonly int defaultFoo = 42;
private int foo;
private bool fooSet;
private static readonly string defaultBar = "default!";
private string bar;
private bool barSet;
public int Foo { get => fooSet ? foo : defaultFoo; set { foo = value; fooSet = true; } }
public bool IsFooSet => fooSet;
public string Bar { get => barSet ? bar : defaultBar; set { bar = value; barSet = true; } }
public bool IsBarSet => barSet;
} |
Beta Was this translation helpful? Give feedback.
-
Yes, but the defaults are not visible to the caller. Sure, I could make your defaultFoo public, but IntelliSense won't show it when someone hovers over MyMethod (or even MyMethodParameters) because it is not linked to foo in any way but name. I appreciate your efforts, but I think I'm out of luck until someone decides to add something like WasDefaulted to the compiler... |
Beta Was this translation helpful? Give feedback.
-
You could attach a This is certainly not as ergonomic as a language feature, but it would achieve the goal stated in this proposal. As for this proposal, I'd say the challenge for you would be in finding a way to specify an implementation that doesn't result in breaking optional parameters. The current implementation is completely unaware that the caller hasn't specified a value for that parameter because, in IL, the caller actually has specified a value. And the signatures of methods with default parameters provide no place to set any flags to indicate otherwise. So at best you'd need syntax to explicitly specify an additional parameter(s) which can convey that information and a way to specify that such parameter is for the passing of which parameters have been explicitly set. And then you'd need to convince the C# team that this comes up often enough to be worth adding to the language. |
Beta Was this translation helpful? Give feedback.
-
Wow. I hadn't thought of using the XML comments, and I have them for all my methods. I'll just add a note to each parameter as to what the default is. Then one of the two solutions above will work. It's not as good as a language feature, but as you say, the chances of convincing the C# team are low. Thanks, everyone, for your contributions! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've read posts that say how the IL generated by the C# compiler for the defaulted parameter syntax doesn't allow the code to determine whether the parameter really was defaulted or the default value was passed. I understand, but what about at the compiler level itself (i.e. if it were built into the language)? Couldn't the IL be generated in such a way as to leave evidence (some flag or something) behind during compilation so that a property like WasDefaulted could exist? For example,
Beta Was this translation helpful? Give feedback.
All reactions