Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

@for can't decrement #691

Closed
hedron-dnd opened this Issue · 9 comments

7 participants

@hedron-dnd

I was a bit surprised to see this isn't possible:

@for $i from 7 through 1 {
    @debug $i;
}

The expected output being:

./test.scss:34 DEBUG: 7
./test.scss:34 DEBUG: 6
./test.scss:34 DEBUG: 5
./test.scss:34 DEBUG: 4
./test.scss:34 DEBUG: 3
./test.scss:34 DEBUG: 2
./test.scss:34 DEBUG: 1

The output, however was nothing.
Was this by design? I can't imagine it would have been.

@robwierzbowski

@for only counts up. Whether this is a bug or not, you can get around it with negative numbers and the abs() (absolute value) function:

@for $i from -7 through -1 {
    @debug abs($i);
}

Result:

/style.scss:3 DEBUG: 7
/style.scss:3 DEBUG: 6
/style.scss:3 DEBUG: 5
/style.scss:3 DEBUG: 4
/style.scss:3 DEBUG: 3
/style.scss:3 DEBUG: 2
/style.scss:3 DEBUG: 1
@hedron-dnd

Thanks @robwierzbowski: Yea, there are various workarounds, they're just less intuitive. I don't know how high of a priority a native implementation would be, but it'd still be nice to see.

@chriseppstein

I think adding a by clause to @for would be very nice. Sure it can always be done via math, but it's a nice sugar.

Examples:

@for $v from 0 through 10 by 2 { ... }
@for $v from 10 to 1 by -1 { ... }

Also, Sass can fairly easily change from increment to decrement when the orientation of the values is flipped. This avoids infinite loops. I'm :thumbsup: on this one.

@nex3
Owner

I'm a little ambivalent about supporting @for ... by. I'm not sure it's widely useful enough to warrant expanding the @for syntax, especially given how easy it is to do by hand.

I do think @for $i from 10 to 1 should work, though.

@dustinhorton

Just ran into this, so if actual use cases help: I was using a for loop to generate a series of max-width media queries, issue being it was generating larger & larger max-width media queries which would simply override all those prior. Fixed using abs() to get a decrementing @for loop.

Can post code if that'd help, but +1 for @for $i from 10 to 1, or the alternate syntax Chris proposed.

@hedron-dnd

The flexibility in the version @chriseppstein posted would be great. As noted; sure, it's easy enough to do most things by hand, but the brevity afforded by some native support ( not to mention the flexibility of something like by in this case ) would be more than welcomed ( and leveraged ) I'm sure.

@Undeterminant

@nex3: If you're going to use the logic that going by 2 is easy to do by hand, going by -1 should be easy enough...

@for $i from 0 through 9 {
  // 10 - $i
}

I think that adding some sort of extension to change the step would be a good thing.

@chriseppstein

I think the by clause is useful, it's obvious what it's doing to an untrained user -- even more so than the math required to accomplish the same result without it and the implementation cost is low.

@robinroestenburg

I added a PR for implementing a decrementing loop (@for $i from 10 to 1 as @nex3 suggested). Sorry I couldn't get hub to add the PR to the issue directly, had a lot of trouble with it - seems there are some problems with Github's new 2FA.

@nex3 nex3 referenced this issue from a commit
@nex3 nex3 Merge branch 'decrementing-loops'
Conflicts:
	doc-src/SASS_CHANGELOG.md

Closes #923
Closes #691
64ce155
@nex3 nex3 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.