dotnet / dotnet/vblang

[Proposal] A dot field lambda syntax

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

Description

Suppose we have this list:
items = new List(Of (Id As Integer, Text As String)()

I suggest using this short lambda syntax:
`Dim x = items.Select(.Id)`

instead of this:
`Dim x = items.Select(Function(item) item.Id)`

More formulas involving other fields are possible:
`Dim y = items.Select(.Id & ", " & .Text)`

Note that can break old codes that uses With blocks, so, we can use one of two solutions:
1. With block has the precedence over short lambdas.
2. The compiler refuses such interference and gives syntax error, so that short lambdas can't be used inside With blocks.

For me, I didn't use the `With Block` for 2 decades and I have no problem of choosing either of the above. But if you don't prefer neither, another way of thinking, is to use another notation instead of `.`, such as:
`Dim x = items.Select(:Id)`
`Dim x = items.Select(@Id)`

We already have a familiar alternative that is ready to be put in use, that is the dictionary lockup symbol:
`Dim x = items.Select(!Id)`

VB lambda syntax is unreasonably too long, and feel bad every time I use them, unlike C#'s:
`var x = items.Select(item => item.Id);`
which is short but still confusing!
I previously suggest an alternative syntax for both languages:
`x = items.Select(Fn(item) => item.Id)`
Which is a compromise between length and readability. II already implemented this ssynatx as a part of my ZML razor syntax.
But, I see now that in many situations, we don't even want such whole decoration. We just want to access a field or combine two fields in a direct syntax, so, why can't we shorten lambdas to a dot field result syntax:
`x = items.Select(.Id)`
and let the compiler generate the whole fancy decorated syntax?
Fore me,
`x = items.Select(.Id)`
is more readable and understandable than:
`Dim x = items.Select(Function(item) item.Id)`

See this function, which I am using in one of my projects, and forced me to think of a solution:
```
Public Sub SetItemsSorce(items As List(Of (Id As Integer, Text As String)))
SetItemsSorce(
items.Select(Function(item) item.Id).ToList,
New ObservableCollection(Of String)(items.Select(Function(item) item.Text))
)
End Sub
```

The part `New ObservableCollection(Of String)(items.Select(Function(item) item.Text))` is not as a vb-style code as should. I wish use the short lambdas like this:
`New ObservableCollection (Of String)(items.Select(.Text) `
And the whole sub becomes:
```
Public Sub SetItemsSorce(items As List(Of (Id As Integer, Text As String)))
SetItemsSorce(
items.Select(.Id).ToList,
New ObservableCollection(Of String)(items.Select(.Text))
)
End Sub
```

Another thing I asked for, is to allow aliasing tuples and the generic syntax:

```
Imports myList = List(Of (Id As Integer, Text As String))
Public Sub SetItemsSorce(items As myList)
SetItemsSorce(
items.Select(.Id).ToList,
New ObservableCollection(Of String)(items.Select(.Text))
)
End Sub
```

For me, this is how a true VB code should appear.

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.