Skip to content

[RFC] Tuple unpacking in loops/iterators #38

@narimiran

Description

@narimiran

Current situation

To iterate through a container, we now have:

for val in mySeq:
  ...

There is also an option to have an index too with:

for i, val in mySeq:
  ...

If mySeq is container of tuples, we currently need to do:

for val in mySeq:
  let
    x = val[0]
    y = val[1]
    z = val[2]
  ...

An example of zip usage:

for val in zip(seqA, seqB):
  let area = val.a * val.b # you must use `a` and `b`, no other letters/words

Proposal

My proposition is to have tuple unpacking available in loops, and to use the same syntax which is currently used for tuple unpacking in let statements: let (x, y, z) = myTuple.

The above example now looks like this:

for (x, y, z) in mySeq:
  ...

or if you want to also use an index:

for i, (x, y, z) in mySeq:
  ...

The example with zip would now allow more meaningful names:

for (length, height) in zip(seqA, seqB):
  let area = length * height

Parentheses must be used to make a clear, understandable difference between for i, val in mySeq and for (x, y) in mySeq, where i is an index, val is a whole tuple, and x and y are elements of that tuple.

This would allow to have a cleaner, nicer and more readable code.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions