Replies: 1 comment
-
I believe that similarity is intentional: dotnet/roslyn#35 (comment)
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
Despite the introduction of
var
, redundancy is still common in field/property and optional argument declarations. There is also scope to simplify member access when the type is implied. Currently we have to write:* Forgive the long lines - that's part of the point!
Existing Propsoals
Extending
var
to be allowed on the left-hand side of these assignments, #601/#1056, is difficult to implement and would, perhaps more importantly, lead to the API being implicit.A well-received proposal to aid in field/property declarations is to allow a "Target-typed
new
expression" #100, which would allowDictionary<string, Func<int>> someMap = new ()
.In my eyes this would produce syntax that would look too close to tuples:
There are several proposals to shorten enum access, #764 which would allow
MyEnumType myEnumTypeValue = .SomeValue;
etc., and #321/#354 which also eliminate the dot:However, that syntax leads to the same ambiguities that can be introduced by
using static ...
but in this case would not be opt-in, allowing code such as:Also, non-local behaviour could easily be altered without a warning if, for example, a property with the same name and type is introduced.
It could also only be used where there is no ambiguity, so does not represent a general, consistent method for shortening enum access.
Note that neither of these proposals remove the redundancy from
String s = String.Empty
. I do not considerusing static ...
to be a solution, for anything!Proposal
My proposal to address these issues is to extend
var
to be used beyond just declarations but also as a general currently-implied-type:var
removes the redundancy whilst maintaining clear intent and only requires an increase in scope of an existing keyword.This change would be semantically consistent with
var
's existing use, in that it would still refer to the currently implied type.It also has precedence in the new parameterless
default
, which would be equivalent todefault(var)
, potentially allowing the implementation to use the same code to deduce the type.Beta Was this translation helpful? Give feedback.
All reactions