Skip to content

Don't skip nil result in loop expression, breaking change #66

@leafo

Description

@leafo

Currently, if you assign a loop to something, or treat it as an expression the result of each iteration is accumulated into an array.

This is very similar to comprehensions, but if the result of an iteration is nil then it is not added to the array.

This makes something like this possible:

my_numbers = {1,2,3,4,5,6}
odds = for x in *my_numbers
  if x % 2 == 1 then x

The obvious downside is that these two things are not the same thing:

out = for x in y
  x

out = [x for x in y]

Since making this decision I've added the continue statement. It works with loop expressions, so the nil check can be done explicitly:

odds = for x in *my_numbers
  continue unless x % 2 == 1
  x

I think it's better to be explicit in cases like this. Therefore I plan to remove this nil check in the loop format.

This is a backwards incompatible change. I've been going through some of my old code and it's kind of annoying finding instances of loops that need to be updated. That's why I'd like some feedback. Maybe this change is not worth it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions