Re: Suggestion for C# language: Modification to the "new" keyword - Allow type inference in C# to use the left-hand side of the assignment

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Anthony,

I can't think of a situation where info flows from the RHS into LHS before the operation on the LHS can complete.

However, using your analogy, it could be said that there are situations where the LHS participates in the equation independently from the RHS. When you use polymorphism:

IComparable<DateTime> dtComparable = new DateTime(2008, 12, 15);

In this case, the LHS is contributing to what the range of values assigned to dtComparable can be (in this case, it expands the range to be anything implementing IComparable<DateTime> not just DateTime, as suggested by the RHS) independent of the RHS. Note that this was always the case (until "var" was introduced).

The introduction of "var" was radically different, in that the barrier which kept each side from influencing the other (since you had to define the range of types on both sides) was broken down.

The new proposal takes advantage of this fact that those barriers were broken down.

Because of that previous segregation, I tend to look at this not in terms of an equation, but in terms of sets (which have no "sides"). With any declaration and assignment statement (like above) you have a set of types allowed on the LHS and RHS. If there is an intersection between the sets, then the assignment is valid. Things like my proposal, "var", even polymorphism just change the range of the set. In that sense, as long as there is an intersection of sets, it doesn't matter what side either is on.

--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Anthony Jones" <AnthonyWJones@xxxxxxxxxxxxxxxx> wrote in message news:OBd$YHPYJHA.5020@xxxxxxxxxxxxxxxxxxxxxxx
"Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:O3bTVuJYJHA.5312@xxxxxxxxxxxxxxxxxxxxxxx
Anthony,

The same issue exists with "var", so while it might not be mathematically correct, it is allowable in the language.

When using "var", the left-hand side of the declaration is completely dependent on the right, it's the same issue, just reversed. Before "var", there was no dependency on either side.


The issue with var is different. We mathematically expect that with an equation like this:-

x = a + b

that x is impacted by the result of the RHS. We do not expect x to participate in anyway in the expression on the RHS. C# 3 allows x to be influenced not only by the value result of a specific invocation of an expression but its type is influenced by the expressions type as well. However the flow is in the same direction, RHS influences LHS but LHS has no part to play in the RHS.


As for what problem it solves, it doesn't necessarily solve a problem (it's a recommendation/suggestion). I personally don't like the use of "var" for the purpose of being concise (as I believe it obfuscates the code), but that's a personal observation (and I believe it is a problem, but wouldn't argue the point, as I know it's pointless).


I do agree and I'm sure we've had a long thread here before on how liberally var should be used. IMO var should primarily used for where the type of the RHS is an anonymous type. Idomatically its reasonable to use it as a result of a LINQ expression (even one where the type is known). The only other place I would use a var is in simple new operations:-

var x = new Thing();

You seem to be proposing that we also be able to do this:-

Thing x = new();

I don't see any improvement with this syntax. In fact it feels wrong because the flow of imformation is weird in fact so far to me its unique. Can you think of any other statement or expression where info flows from the RHS into LHS before the operation on the LHS can complete?


--
Anthony Jones - MVP ASP/ASP.NET


.



Relevant Pages

  • Re: Suggestion for C# language: Modification to the "new" keyword - Allow type inference in C# t
    ... The same issue exists with "var", so while it might not be mathematically correct, it is allowable in the language. ... When using "var", the left-hand side of the declaration is completely dependent on the right, it's the same issue, just reversed. ... We do not expect x to participate in anyway in the expression on the RHS. ... However the flow is in the same direction, RHS influences LHS but LHS has no part to play in the RHS. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Generic methods..
    ... T Product(T lhs, T rhs); ... T Negate(T arg); ... private static readonly IMathprovider; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: why is it so ?
    ... >>> (lhs, rhs) ... > Could you please tell me by which rule you have interleaved evaluation ... method of breaking up expressions using partial ordering by creating an ...
    (comp.lang.c)
  • Re: Suggestion for C# language: Modification to the "new" keyword - Allow type inference in C# t
    ... The RHS is no longer independant of the LHS and that is just ... Why oh why must you write MyClass TWICE: ... IF this is an assignment statement ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A good ride, ruined
    ... straight into it, on the LHS, knocking it over onto its RHS. ... always, the superficial damager didn't ...
    (uk.rec.motorcycles)