This project is read-only.

More functional With .. End With

Topics: VB Language Design
Apr 21, 2014 at 2:10 AM
Two more With constructs:
        With expr IsNot Nothing
            ....
        End With
and more general
        With expr If expr
            ...
        End With
May 4, 2014 at 10:18 AM
What do they do?

-ADG
May 5, 2014 at 4:11 PM
Edited May 5, 2014 at 4:19 PM
Thinked more and refined my improvements.

Checking for nothing.
Current With:
Dim obj = GetMsgObj()
if obj IsNot Nothing Then
    With obj
                MsgBox (.Message)
    End With
End If
Improved With:
With Not Nothing GetMsgObj()
    MsgBox (.Message)
End With


Checking for any condition except nothing.
Current With:
With GetMsgObj() 
    If .Ready = True Then
        MsgBox (.Message)
    End If
End With

Imporved With:
With GetMsgObj() If .Ready = True
    MsgBox (.Message)
End With


Combined together, checking for any condition.
Current With:
Dim obj = GetMsgObj()
If obj IsNot Nothing AndAlso obj.Ready Then
    With obj
        MsgBox (.Message)
    End With
End If
Improved With:
With Not Nothing GetMsgObj() If .Ready = True
    MsgBox (.Message)
End With

Intead of Not Nothing can be used NotNothing, but i like it separated :)
May 5, 2014 at 5:04 PM
I'd like to see this sort of thing available as a function, rather than just as a statement. The ?. syntax people are proposing is really more like a with-if. Foo?.Bar is really more like either With(Foo: Default Nothing Else .Bar) or With(Foo: .Bar Default Nothing) depending upon whether it's more convenient to have the default first or last. To allow for nested usages, the "default-at-end" syntax would allow With(Foo: .Bar: .Boz Default Nothing); each : would interpret the thing to its left as the argument for a new With.
May 8, 2014 at 4:00 PM
@codefox I'm also interested to see how the null propagating operator might play into this. I do like the abbreviated syntax of what you are proposing.