dotnet / dotnet/vblang

[Proposal] improvements the ! operator for the anonymous variable

Open
#129 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

Currently in VisualBasic, that we can using the ``!`` operator for the default index property. When the ``!`` operator combine with the VisualBasic ``With`` anonymous variable syntax, it give us too much convenient in the data analysis programming:

```vbnet
.GetValue("value") = 123
' which is equivalent to the code
!value = 123
```

Using ``!`` operator makes our code more brief as it let us without typping the additional ``GetValue`` name when it combine with the ``With`` anonymous variable syntax

But in a more common scenario, we using a string variable instead of the string constant, like the example as:

```vbnet
For Each group$ In {}
.GetValue(group) = .GetValue(group) * 10
Next
```

As the ``!`` operator just enable applied on the string constant, so that in such scenario, we are not able using ``!group`` for getting the value any more, as the code ``!group`` it means ``.GetValue("group")``, not means ``.GetValue(group)``.

Due to the reason of we are using ``()`` brackets for passing the variable parameter, ``()`` brackets which have a underlying meaning of ``Evaluation``, so that I think that we can using syntax ``!(group)`` for the string variable and ``!group`` for a string constant, as the ``!(...)`` means evaluate the variable.

The ``!`` operator combine with the ``With`` anonymous variable syntax is a common scenario and much useful language feature in some data analysis application, example like the source code:

+ https://github.com/xieguigang/GCModeller/blob/0848e3691553f71bf0f9b8b7ba06680d554c35f3/src/GCModeller/CLI_tools/eggHTS/CLI/0.%20Samples-Expressions.vb#L171
+ https://github.com/xieguigang/GCModeller/blob/6f98a59a3578005e1c4ba747d91dc4c97ff1b469/src/GCModeller/models/Networks/STRING/FunctionalNetwork/FunctionalEnrichmentNetwork.vb#L249

```vbnet
' Creates an anonymous variable
With New EntityObject(name)

For Each group As String In groupLabels.Keys
Dim experiment = totals(group)
Dim relative# = experiment.proteins(protein.ID) / experiment.total * 100%

.ref(group) = relative
Next

!AVERAGE = (Aggregate s In .Properties.Values Into Average(Val(s)))
!uniprotID = protein.ID
!fullName = uniprots(protein.ID) _
!fullName

' reference the anonymous variable its original source
relativeAmounts += .ref
End With

'''
''' Extension method for VisualBasic ``With`` anonymous variable syntax source reference helper
'''
'''
'''
'''
Public Function ref(Of T)(x As T) As T
Return x
End Function
```

Allows using the ``!(...)`` for the index property using a variable will makes the code more better:

```vbnet
For Each group As String In groupLabels.Keys
Dim experiment = totals(group)
Dim relative# = experiment.proteins(protein.ID) / experiment.total * 100%

.ref(group) = relative

' In a more brief way
!(group) = relative
Next
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.