Skip to content

Multiple For or For Each Control Variables Per Statement #48

@AdamSpeight2008

Description

@AdamSpeight2008

For ... Next Loop

Given the following section of code, it can be written as

For x = 0 To 9
  For y = 0 To 9
    Console.WriteLine($"{x} * {y} = {x*y}")
  Next
Next

or

For x = 0 To 9
  For y = 0 To 9
    Console.WriteLine($"{x} * {y} = {x*y}")
Next x. y

Yet we can not write

For x = 0 To 9, y = 0 To 9
    Console.WriteLine($"{x} * {y} = {x*y}")
Next x. y

or

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

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

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions