[Mercury Feature Request] Keyword Dim - Support for multiple declarations and initialisations

Actually
Dim test1, value2, result as Integer
works in vb, but
Dim test1, value2, result as Integer = 5
does not, because multiple variables can’t initialised at the same time to the same value.
It would be an interesting feature, me thinks.

This should then also work (infer):

Dim Test1, value2, result = 5

But this is already valid VB with option Strict Off, where Test1 = nothing (object), value2 = nothing (object) and result = 5

I like this proposal, but it conflicts with the existing syntax.

If type is declared like Integer, it can’t be nothing. So a solution can be that when a type is declared, or when infer is OFF, value is given to all members of declaration.

Or may be use the := for this to create a distinct difference

This should also be usable with the already proposed LET function, to set a list of variables to the same value (or even property or function result)

In python this is done like:
a = b = 100
So in Mercury it can be
Dim Test1 = value2 = result = 5 or
Dim Test1 = value2 = result as Integer = 5
Please note that for value types, these are 3 different instances, for reference types it’s 1 instance.
e.g. Dim myClass1 = myClass2 as myClass = New myClass

1 Like

Integers can still be Nothing (i.e. 0) as I understand it? SO this would conflict for all types (and even if not, it would be confusing if it initialized all integers, but not all vars of a reference type.

As a side Q, would Dim x, y, z = New Foo() initialize all three to the same Foo instance, or would New Foo() be evaluated three times?

I think this needs deeper thought

zero is not nothing. Zero is a number.

If the type is Integer, then Nothing = 0
Because Nothing is Default(Of T) and Default(Of Integer) = 0

Good point. Indeed raises questions.

I humbly disagree: Default(Of Integer) = 0 And Default(Of T)=Nothing does not equate to Nothing = 0
Those are two different, disjoint statements.
If the type is Integer, then Default(Of Integer) = 0
Value types don’t ever default to nothing.

Try the following code in VB:

Dim i As Integer
If i = 0 Then
    Msgbox("Hello")
End If
If i = Nothing Then
   MsgBox ("Hello 2")
End If
i = Nothing
If i = 0 Then
   Msgbox("Hello3")
End If
If i = Nothing Then
   MsgBox ("Hello 4")
End If

And some references about the working of Nothing by the former Visual Basic PM:




That’s not how Nothing was explained to me. I understand Nothing is different than “Null” int C# or “nil” in Pascal & Swift, in that Nothing is default(T), so it is zero, for Integers?

Exacty, see the references I gave.

1 Like