Allow var
use with optional arguments.
#8735
Replies: 14 comments
-
This is closely related to the idea of allowing Given the optional parameters are (currently) required to be constants, one could perhaps argue that the type will always be obvious, but can still be a function of other code. Note that The benefit of shortening the type (when the default value will no doubt be longer) in a select number of cases seems minimal, and also opens up some opportunity for misuse/abuse when it is by no means warranted; duly, I would be disinclined to support this proposal presently. |
Beta Was this translation helpful? Give feedback.
-
This is also linked to #354 and #764, which would potentially allow the following, respectively: void DoThis(PartialResultProcessing processing = NoPartialResultSupport);
void DoThis(PartialResultProcessing processing = .NoPartialResultSupport); Though both only apply to enums, although the second could be expanded to general member access, allowing, for example, the following: void DoThis(string str = .Empty); |
Beta Was this translation helpful? Give feedback.
-
I believe that allowing enums would require a significant change in how default parameters are implemented; I believe they essentially bake constants into the assembly at the moment, but enums can be exposed by an API and change, so I presume that would require some effort. |
Beta Was this translation helpful? Give feedback.
-
See "Why no var on fields?" by Eric Lippert for a good description of why Also, see #601 for [my] proposal for allowing them and the resultant discussion. |
Beta Was this translation helpful? Give feedback.
-
@DavidArno thank you kindly: I knew it had been discussed somewhere... Clearly a number of the issues presented in those 2 resources do apply; though, the situation it not quite 'as bad', as these parameters are the 'end of the chain' of misery. I do think that the benefit (if there is any) of this proposal is too minimal to justify the issues associated with maintaining an implicitly declared external API (ignoring the cost-of-implementation concern). |
Beta Was this translation helpful? Give feedback.
-
So it seems using One option that I don't think has been suggested yet is to also allow var someMap = new Dictionary<string, Func<int>>();
Dictionary<string, Func<int>> someMap = new var(); This would address this issue as well as ones such as #601 and #100, while hopefully being implementable and leaving the API explicit. void DoThis(PartialResultProcessing processing = var.NoPartialResultSupport); and also potentially address #354/#764: type.GetProperties(var.Public | var.Static);
...
if (myEnumTypeValue == var.SomeOtherValue || myEnumTypeValue == var.SomeValue)
{
... This change would be semantically consistent with |
Beta Was this translation helpful? Give feedback.
-
@rawlinson How about #100? Using |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h #100 does not address this issue as it only targets the use of Expanding the use of |
Beta Was this translation helpful? Give feedback.
-
I much prefer the syntax suggested by #321. When not ambiguous you can simply omit the name of the enum: void DoThis(PartialResultProcessing processing = NoPartialResultSupport);
...
type.GetProperties(Public | Static);
...
if (myEnumTypeValue == SomeOtherValue || myEnumTypeValue == SomeValue) |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I generally prefer things to be qualified and think #321 would add ambiguity, if not for the compiler then for the reader. What would happen with the following: BindingFlags Public = BindingFlags.Public | BindingFlags.Static;
type.GetProperties(Public | FlattenHierarchy); I don't see a way of resolving it that wouldn't require me to look closely as each case where the type is omitted. Using Also, what about field access, should all member names be allowed to be 'shortcutted'? I also dislike #100, the syntax looks too close to tuples for me. Again, using
I think a new issue should be raised to propose the change to |
Beta Was this translation helpful? Give feedback.
-
Just to note, you can already shorten enum member access via using System;
using System.Reflection;
using static System.Reflection.BindingFlags;
public class C {
public void M() {
Type type = typeof(C);
BindingFlags Public = BindingFlags.Public | BindingFlags.Static;
type.GetProperties(Public | FlattenHierarchy);
}
} |
Beta Was this translation helpful? Give feedback.
-
@HaloFour In that case the behaviour is opt-in. And I am tempted to propose removing |
Beta Was this translation helpful? Give feedback.
-
I have added a new issue for the new proposed behaviour: #1063. |
Beta Was this translation helpful? Give feedback.
-
Related: https://gafter.blogspot.com/2017/06/making-new-language-features-stand-out.html |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Optional argument declarations can get quite long, especially when using enums or generic types. Allowing the use of
var
would shorten the declarations making the code more readable and reduce redundancy. Compare:Beta Was this translation helpful? Give feedback.
All reactions