Req7: Have separate syntax for assignment "=" and comparison "=="
[This post is part of a series, "wish-list for future versions of VB"]
IDEA: Have different syntax for assignment "=" or ":=", versus comparison "==". Currently "=" is used for both. Bill McCarthy writes, "To determine which one it means, you have to look at not just the operands but the context of the entire statement". He points out that it's even more confusing with single-line lambdas:
Dim x = 5
Dim f = Function(i) x = i ' this "=" means an equality-test
Dim g = Sub(i) x = i ' this "=" means an assignment
This issue will become more pressing if VB decides to add "inline assignment", i.e. an expression which performs an assignment. Here are the tricks that C# can do with inline assignment, and the workarounds that VB is forced to use:
// C# tricks...
readonly string p { get { return _p ?? (_p = Expensive()); } }
string x, y, z;
x = y = z = Expensive();
' VB workarounds...
ReadOnly Property p As String
Get
If _p Is Nothing Then _p = Expensive()
Return _p
End Get
End Property
Dim z = Expensive(), y = z, x = z
Note incidentally that VB already uses := for assignment to named parameters.
Provisional evaluation from VB team: The inline assignment examples are not compelling: it's best that each statement have at most one side-effect. Read also Eric Lippert's blog where he explains why C# inline assignment is more confusing that it looks. As for the point that it's hard to read which is which, we agree, but feel this would best be addressed by the IDE as a rendering-issue rather than by changing the syntax of the language.
Comments
Anonymous
February 12, 2010
Thank you! I know it may seem odd to be grateful for not getting a feature, but mixing assignments and expressions is just begging for trouble.Anonymous
February 12, 2010
No inline assignment please, it causes too many bugs due to it being misread.Anonymous
February 14, 2010
My vote FOR inline assignments. In fact VB currently treats assignment as statement not as expression. I like the possibility to treat assignment as expression (something that produces value and we can then work with the value. Well it might be confusing a little, but this is as well: Dim x = a = b For compatibility of existing code, we cannot change meaning of the = operator to become assignment in all contexts. I suggest leaving behavior of = unchanged and introducing a new assign operator for inline assignment. The best chois is IMO :=, but I'm afraid it cannot be used because of possible confilicts when passing parameters by name like this: Sub DoSomething(ByVal x%) End Sub Sub Test() Dim x = 5 DoSomething(x := 7) 'What does it mean? End Sub So, we can introduce other operator like "<-". To avoid confusion, we can also introduce a new operator for comparison ==. Maybe we can also introduce a new operators === and !== (or <!> or <<>> ? ;see JavaScript or PHP). Dim a$ = "" Dim b$ = Nothing Dim c = a = b 'Dim c As Boolean = True Dim c = a == b 'True Dim c = a === b 'False Dim c = a <- b ' a = b : Dim c As String = Nothing 'Late binding: Dim xc As Object = "x"c Dim xs As Object = "x" Dim c = xc = xs 'True Dim c = xc == xs 'True Dim c = xc === xs 'False Dim c = xc <> xs 'False Dim c = xc <!> xs 'TrueAnonymous
February 15, 2010
I don’t like inline assignment, but separate operators for assignment and equality would definitely make code more readable – I’ve never liked the VB way of overloading the meaning of the = operator. It’s probably too late to change this now though – even with full backwards compatibility, it would just create confusion.Anonymous
February 24, 2010
Surely VB is VB and people shouldn't try to alter its syntax to match C#'s or Delphi's.Anonymous
February 24, 2010
The idea of somehow distinguishing the two types of = via rendering seems good. Perhaps the IDE could simply use different colour-coding for each type of =, but they could continue to be displayed and used as a single = sign as at present.Anonymous
March 01, 2010
The comment has been removedAnonymous
March 09, 2010
I'm not a fan of this idea. VB is VB, and this is just the way it does assignment and equality. The confusing lambda examples at the beginning aren't, in my opinion, a result of the = serving two purposes, but the poor syntactical way lambda expressions were implementing in VB .NET.Anonymous
January 11, 2011
It's never gonna happen, we all know that so why waste on explanations, I do think it would be a good idea (potentially this would allow production of the assigned value like in C#, and so implementation of ++x/x++ (dec/inc pre/post) operators). But I guess there are things that you can't just change.Anonymous
January 09, 2012
Www.freeiteducation.com to learn VB