This project is read-only.

Assignment expressions that return value

Topics: VB Language Design
May 5, 2014 at 7:04 PM
In the C# (and C-like languages) there is assignment expressions that return value, for example:
while ((line = reader.ReadLine()) != null)
{
    Console.WriteLine(line);
}
In VB the you have to write such While loop with something like:
line = reader.ReadLine()
While line IsNot Nothing
    Console.WriteLine(line)
    line = reader.ReadLine()
End While
The propose is to implement assignment expressions that return value in VB through the use of the Let keyword:
While (Let line = reader.ReadLine()) IsNot Nothing
    Console.WriteLine(line)
End While
The example is pretty straightforward, but became more evident the need when you try to use the Continue keyword in such algorithm. Here is an hypothetical example:
line = reader.ReadLine()
While line IsNot Nothing
    ...
    If line.Length = 0 Then
        ' I could use GoTo to avoid duplicate code!
        line = reader.ReadLine()
        Continue
    End If
    ...
    Console.WriteLine(line)
    line = reader.ReadLine()
End While
Other examples:
If (Let x = getSomething()) > 0 Then
    doSomething(x)
End If
...
foo(Let y = bar())
Note: Initially I though in the := operator, but lead to a conflict with named argument passing when used in method/function call.
May 5, 2014 at 8:22 PM
I like this, though in many cases if multi-part expressions are added, one could say (x = getSomething() : x).

I would like to see Let as a language feature, and have it be usable in this case, but would like to see it as a variation on Dim which would allow an identifier to be reused within a scope in certain cases where such reuse would be unambiguous, but would forbid subsequent assignments to the identifier.

In particular, a variable written by a Let statement at one place in the code should be usable at another place in the code only if no possible execution path between the two places would encounter another Let with the same variable name.

There are many situations where it's necessary to assign a variable (whether in an expression or otherwise), use it shortly thereafter, and then never use it again. Semantically, I would regard:
Let temp As Integer=(x1-y1)
Foo = temp*temp
...
Let temp As Integer=(x2-y2)
Foo = temp*temp
...
to be cleaner than either
Dim temp1 As Integer=(x1-y1)
Foo = temp1*temp1
...
Dim temp2 As Integer=(x2-y2)
Foo = temp2*temp2
...
or
Dim temp as Integer

temp=(x1-y1)
Foo = temp*temp
...
temp=(x2-y2)
Foo = temp*temp
Returning to your examples, if there was no need for the variable x before the if statement, a Let as described would allow its scope to extend beyond the expression, while a Dim within the expression would not, and a `Dim before the expression would create a variable whose scope would likely be excessively extended.
May 6, 2014 at 1:16 PM
I understand what you mean, and I think worth a discussion.

Anyway, I found that my proposal just lead to the 34. Declaration expressions in the VB LDM 2014-02-17 discussion.