dotnet / dotnet/vblang

Multiple `For` or `For Each` Control Variables Per Statement

Open
#48 21 comments 2 reactions 0 assignees View on GitHub
Approved-in-Principle Proposal Prototype-Needed
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

## `For ... Next` Loop
Given the following section of code, it can be written as
```vbnet
For x = 0 To 9
For y = 0 To 9
Console.WriteLine($"{x} * {y} = {x*y}")
Next
Next
```
or
```vbnet
For x = 0 To 9
For y = 0 To 9
Console.WriteLine($"{x} * {y} = {x*y}")
Next x. y
```
Yet we can not write
```vbbet
For x = 0 To 9, y = 0 To 9
Console.WriteLine($"{x} * {y} = {x*y}")
Next x. y
```
or
```vbnet
For x = 0 To 9, y = 0 To 9
Console.WriteLine($"{x} * {y} = {x*y}")
Next ' equivalent to Next y : Next x
```
We should enable the last two scenario to compile.
**Note:** For what if the inner code contains `exit` `continue` `

--------

## `For Each ... Next` Loop
**Premise**
The premise of this feature is to allow the user to specify multiple iterators on a single `For Each`.

**Basic Structrure**
```
For Each x In xs, y In ys
' ...
' exit ' exits ys iterator
` exit x ' exits xs iterator
' continue ' goto next iteration of ys
' continue x ' goto next iteration of xs
' Next ' an error? / goto next iteration of ys then xs?
Next y , x
```

Example

```vbnet
Public Overrides ReadOnly Property AdditionalLocations As IReadOnlyList(Of Location)
Get
Dim builder = ArrayBuilder(Of Location).GetInstance()
For Each sym In AmbiguousSymbols, l In sym.Locations
builder.Add(l)
Next l, sym
Return builder.ToImmutableAndFree()
End Get
End Property
```
semantically equivalent to the following
```vbnet
Public Overrides ReadOnly Property AdditionalLocations As IReadOnlyList(Of Location)
Get
Dim builder = ArrayBuilder(Of Location).GetInstance()
For Each sym In AmbiguousSymbols
For Each l In sym.Locations
builder.Add(l)
Next
Next
Return builder.ToImmutableAndFree()
End Get
End Property
```
~**Edit** `For Each` already supports this syntax.~

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.