Proposal: var class member when using auto-completion #1857
Replies: 6 comments
-
Variations of this has been suggested many times - for fields, methods, properties, etc - but I'm having trouble finding references to the duplicates - var, field and member are very commonly used. The approach being taken by the LDM - and currently scheduled for C# 8.0 - is called Target-Typed new() #100.
|
Beta Was this translation helpful? Give feedback.
-
@SymmetryKeeper-Y From what I understand, this isn't just a design choice, but it is impossible to not use
Not really. And public void DoSomething(Dictionary<string, int> dict) { ... }
...
DoSomething(new()); And your code only works with parameterless constructors which is, like you said, "some specific cases". With public Foo { get; set => field = value ?? new(bar, baz); } To make sure |
Beta Was this translation helpful? Give feedback.
-
@willard720
As described to me on Gitter the issue lies largely in how Roslyn was implemented as a concurrent and non-blocking compiler allowing multiple threads to query into the semantic model simultaneously with the expectation that they would each get back consistent results. That makes it difficult to make the kind of circular inferencing detection mechanism that |
Beta Was this translation helpful? Give feedback.
-
Exactly. IMO, fields would definitely be doable, but would have engineering challenges associated with them. @gafter would understand the details the best. The part that gets into "super complex, to the point that it's highly unlikely it could ever warrant the costs" is if you wanted "var" return types for methods. With how methods interact with inheritance/implementations, it becomes super complex that figuring out signatures of methods now involves needing to understand bodies (which also need to deeply understand the inheritance/implementation shape of their surrounding type). -- My personal opinion though is that it's more desirable to have |
Beta Was this translation helpful? Give feedback.
-
@SymmetryKeeper-V Also, couldn't you say the same thing about using numbers or bool flags in general? SetDogAge(new AgeMeasuredInDogYears(5));
SetDogAge(age: new(5));
SetDogAge(new(5)); Given the method age, it's pretty clear, and we can still add the parameter name to better explain what's going on. Frankly, I think we should care more about people using IDE's that can show that information, as that's where code is viewed most often. Besides that, anywhere online, StackOverflow, online blogs, or textbooks are either to likely not use it, or show the method decleration as well that shows the type of the argument. They usually already have that context. And if it's really that confusing then they just won't use it. Why should we have to not allow a feature just because some dude might write a blog and use that feature and it's a tad bit more confusing? |
Beta Was this translation helpful? Give feedback.
-
@willard720 That's a great argument. I use named arguments all the time already, and |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Possibility to use var on class members and properties when using auto-implementation.
Motivation
Less typing and shorter lines in class declarations.
Design
Beta Was this translation helpful? Give feedback.
All reactions