dotnet / dotnet/vblang

[Proposal] Selector expressions

Open
#596 4 comments 0 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 an array named arr, and want to select items > 3:
```VB.NET
dim result = arr.Select(function(x) x > 3)
```

I want to rewrite it like this:
```VB.NET
dim result = arr[#Item > 3]
```

Note:
In #556, I asked for allowing [] for arrays and indexer. I am using this here to avoid any confusion with any old code using `()`.

It items are objects with properties:
```VB.NET
dim result = Students.Select(function(x) x.Name)
```

then:
```VB.NET
dim result = Students[#Item.Name]
```

Which can be shorten to:
```VB.NET
dim result = Students[#Name]
```

note that I previously wanted to use dot instead of #, but this can cause some confusion inside With Blocks.

Now, Suppose we have a list like this:
```VB.NET
Dim Students as List(Of Student)
```

lets complicate the selector a little:
```VB.NET
dim result = Students.Select(function(x) x.Address.Street(0) = "a" OrElse x.Address.Street(0) = "a")
```

or with LinQ:
```VB.NET
dim result = from x in Students
Let startsWith = x.Address.Street(0)
Where startsWith = "a" OrElse startsWith = "a"
Select x
```

We can make this shorter by defining a var for the selector and reuse it:
```VB.NET
dim startsWith = #Student.Address.Street(0)
dim result = Students[startsWith = "a" orelse startsWith = "b" ]
```

Note using the type #Student so we avoid late binding and have intellisense support.
which can be more compact if VB always the in array syntax:
```VB.NET
dim result = Students[startsWith in {"a", "b"}]
```

or in another way (since the selector is an expression tree, if can be some sort of a function delegate expression:
```VB.NET
dim startsWith = #Student.Address.Street.StartsWith
dim result = Students[startsWith("a") orelse startsWith("b") ]
```

and if add a StartsWithAny extension method to the string class:
```VB.NET
dim startsWith = #Student.Address.Street.StartsWithAny
dim result = Students[startsWith({"a", "b"}) ]
```

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.