Propose "With" Block expressions
- Dominant language
- No language data
- Stars
- 328
- Forks
- 71
- PR merge metrics
- No merged PRs in 30d
Description
@AnthonyDGreen proposal a block expression for makes the Vb programming in a more brief way in this issue: **Block expressions(https://github.com/dotnet/vblang/issues/109)**. The syntax that suggested by @AdamSpeight2008 is my favorite, as it avoid some unnecessary ``{...}``, and makes the code more brief:
```vbnet
Dim timeOfWeek = Select Case (day As DayOfWeek)
Case DayOfWeek.Monday To DayOfWeek.Friday
"Work Week"
Case DayOfWeek.Saturday, DayOfWeek.Sunday
"Weekend"
End Select
' too verbose
Dim timeOfWeek = Function(day As DayOfWeek) As String
Select Case day
Case DayOfWeek.Monday To DayOfWeek.Friday
Return "Work Week"
Case DayOfWeek.Saturday, DayOfWeek.Sunday
Return "Weekend"
End Select
End Function
```
I think the VB language can introduce another block expression for makes the code more brief:
## Propose ``With`` Block expressions
There is a lot of space for improvements on the VB.NET, for makes more brief and elegant. One of these space is what we can improvements on a VB6 inheritance, the VisualBasic exclusive ``With`` language feature, which is my most favorite one. Here is what I'm summarized for the ``With`` expression, that probably can makes the VisualBasic more elegant:
1. **Initialize With (block expression)**: when the ``With`` keyword combine with a ``New``
2. **Modify With (block expression)**: when the ``With`` keyword combine with an object expression
3. **Map With**: when the ``With`` keyword combine with a property
### Initialize With
Here is what we can do in current version of VisualBasic:
```vbnet
' too verbose
Dim o = New Foo
o.a = 1
o.b = 2
o.c = 3
' Works on current version
' elegant way
Dim o = New Foo With {
.a = 1,
.b = 2 + .a,
.c = 3
}
```
### Modify With
The ``Modify With`` block expression is a syntax that somekind of likes the ``New With``, it allows us modify the object with member value and then returns the original object. In a more brief way:
```vbnet
' Works on current version
' traditional way
' not elegant
Function Modify_a_example(Optional trace$ = Nothing) As Foo
Dim a As Foo = Foo_create()
a.Trace = trace & $" at line {a.LineNumber}"
Return a
End Function
' Works on current version
' anonymous variable way
' not elegant
Function Modify_a_example(Optional trace$ = Nothing) As Foo
With Foo_create()
.Trace = trace & $" at line {.LineNumber}"
Return .ref()
End With
End Function
' Or using the block expression like the New With statement
' Modify with means modify the member value, from a exists object
' and then returns this object
' elegant way
Function Modify_a_example(Optional trace$ = Nothing) As Foo
' Just somekind of like the new with expression
' Return New Foo With {...}
Return Foo_create() With {
.Trace = trace & $" at line {.LineNumber}"
}
End Function
```
### Map With
Sometimes, we are writing a class property just want to expose part of the internal object to the public, so that in traditional way, we must write additional code for this job. But I'm a lazy programmer, I didn't want to write such additional code at all. For helping the lazy programmer like me, an issue that solving this property mapping is open in the early time at here: **>> https://github.com/dotnet/roslyn/issues/15708 <<**
```vbnet
' Using this private object for stores the parameters
Dim o As Foo
' Adjust the parameter value through a property mapping
' Binding the Property InnerValue with (Foo) o object its property a
Public Property InnerValue As String With o.a
Public ReadOnly Property InnerValue As String With o.a
Public WriteOnly Property InnerValue As String With o.a
' With means map the variable a in object o to this property: InnerValue
' traditional property is too much additional code
' Not brief
Public Property InnerValue As String
Get
Return o.a
End Get
Set
o.a = value
End Set
End Property
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.