Replies: 6 comments
-
-1 for the |
Beta Was this translation helpful? Give feedback.
-
👍 for the idea of fields being able to use 👎 👎 👎 👎 👎 👎 👎 👎 👎 👎 👎 👎 👎 👎 👎 for the idea of the |
Beta Was this translation helpful? Give feedback.
-
Inferring type from multiple expressions for a mutable variable is not that feasible I guess. |
Beta Was this translation helpful? Give feedback.
-
If it was totally feasible I'd be just as against it. There is great value to being able to understand a name in context with its type and value all in one place without scanning through the method body and it encourages code that is more self-documenting. |
Beta Was this translation helpful? Give feedback.
-
On this surface it seems like this is not different than figuring out the return type of a statement lambda. However, since its a variable that can be referenced within the body of code itself, it becomes a bit more complicated. Otherwise, I'm always in favor of more var. |
Beta Was this translation helpful? Give feedback.
-
Because I also opened a discussion about it (see #6200) and this subject still keeps me thinking, I would like to add something additional: The Rust programming language already supports this feature. Rust's type inference is way more powerful than C#'s in soo many aspects. Rust infers both in forward and backward directions, and also subparts of types (you can often also fill in underscores for generic parameters, for example when you need to create a Vec but you want to have the generic type inferred), all saving you from tons of typing. I am not saying that C# should completely follow all this, but at least it might be a good idea to look around a bit more how other languages solve things here, to see what's possible. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
@eamodio commented on Mon Apr 20 2015
Currently
var
can only be used in cases where the declaration and assignment are linked. It would be great (imo) ifvar
could be used even if those statements weren't linked but was still verifiable by the compiler (not looking for dynamic here or anything like that).This would be great for cases like the following silly example:
Before:
After:
It would be even more awesome if the compiler could infer even more - maybe using some structural typing when generating the anonymous, like:
@aluanhaddad commented on Wed Apr 22 2015
The example you provide can be easily simplified with the use of the ?: operator.
That said there are cases where it could be useful, such as in try - catch - finally blocks, but it would have some unpleasant consequences. For instance, the type of the variable cannot be referred to until it has been assigned. Also, the type becomes path dependent so the compiler would need to do flow analysis to make sure that all possible assignments resolve to the same type.
@eamodio commented on Wed Apr 22 2015
Yeah - the example was certainly the most simple version and could be replaced with ?:, but you can imagine much more complex versions, try/catch as you mentioned, usings, multiple ifs, switch statements, etc.
Why couldn't the type of the variable be referred to? The variable isn't dynamic - it would still be statically known through some compiler magic ;) -- hopefully only needing limited flow analysis over what is currently done for var. And any case the compiler didn't want to support (because of complexity or whatnot) it could just issue an error that var couldn't be used in that case.
While I think the simple var case is nice, my guess the bang for the buck wouldn't necessarily pay off (but wanted to raise it as it would be nice and who knows might be easy). Though I think the really valuable feature would be "structural" typing with anonymous objects. That imo would be a huge win and isn't just syntactical sugar.
@aluanhaddad commented on Sat Apr 25 2015
@eamodio
To your first point:
Yes, there are indeed more cases than just try - catch - finally, an expression form of pattern matching would solve most of these issues (see below).
To your second point:
var
does not need to use flow analysis as it currently stands. It simply infers the type of the variable as the type of whatever expression is on the right hand of the=
. I agree it has nothing to do with dynamic typing. I was suggesting the flow analysis would be complex, especially for tools like intellisense.To your third point:
I think some form of structural typing would be great. The current proposals for tuples, which are expected to have nameable components, might give just this functionality. Also, the proposal for pattern matching, which currently includes property pattern matching, would also benefit in this area.
@GeirGrusom commented on Mon Apr 27 2015
You can't use
var
with unsafe code either.@GSPP commented on Sun Oct 25 2015
Also consider
var
on fields.Beta Was this translation helpful? Give feedback.
All reactions