Repository navigation
Description
Recently there was a disscussion (two questions at once actually) at the stackoverflow, one of those boring "how to increment a variable?" (i.e. #1545) So I came up with this useless (as I thought first) snippet:
// implementation:
.counter() {
.counter-redefine();
.counter-wrapper((@counter + 1));
}
.counter-wrapper(@new: 0) {
.counter-redefine() {
@counter: @new;
}
} .counter-wrapper();
// usage:
a {
.counter();
counter: @counter;
}
b {
.counter();
counter: @counter;
}
c {
.counter();
counter: @counter;
}
d {
.counter();
counter: @counter;
}
Result:
a {
counter: 0;
}
b {
counter: 1;
}
c {
counter: 2;
}
d {
counter: 3;
}
As far as I understand it's even two issues (or at least "side effects") abused here:
.counter-redefinecalled inside.counteris the one expanded to the global namespace by initial.counter-wrappercall in the global scope and not the one expanded by the next.counter-wrapperlocal scope call (the latter is not visible yet, see Use Dynamically Generated Classes as Mixins #1399 (comment) second example)..counter-wrappercall does not actually define new or modify old@countervariable so LESS does not detect anyRecursive variable definition for @counterat the.counter-wrapper(...)stage, what.counter-wrapperdoes modify is the@newargument variable which seems to behave somewhat like a "singleton" variable regardless of the.counter-wrappercall scope. So next.counter()->.counter-redefine()call defines new@countervariable initialized with the previous@newargument value. This might be considered not an issue at all (or a very minor one at least), but when it's combined with the first one... hmm...
In other words, what we have here is simply:
@new: 0;
@counter: @new;
@new: @counter + 1;
@counter: @new;
@new: @counter + 1;
@counter: @new;
@new: @counter + 1;
// etc.
And LESS does not detect any 'recursive variable definition' just because of recursive mixins and argument variable evaluation black magic.
I doubt LESS should really detect the `recursive variable definition' in the initial snippet, but the "issue" (2) looks interesting on its own, notice the following example:
// Mixin call in .a's scope changes behaviour of a mixin unlocked in any other scope.
.Person(@gender) {
.sayGender() {
output: @gender;
}
}
.Person('Male');
a {
.Person('Female');
}
b {
.sayGender();
}
I.e. the b scope knows nothing of the a scope, nevertheless it still can use the foreign scope value.
Activity
scottgit commentedon Nov 26, 2013
Regarding your final example
We can see how order is important in this case of LESS calculations. In your example,
bgeneratesoutput: 'Female'as defined bya, but ifbis moved beforea, the output isoutput: 'Male'. So it is not thata"changes behaviour of a mixin unlocked in any other scope," but rather, it changes the behavior of all mixins used in scopes following it.Regarding your counter idea
You actually don't even need the extra
initcode, as this works too:You can actually make the counter mixin more generic, and capable of handing multiple counter routines through some creative parametric mixins and variable variable calls (tested on LESS 1.4.1):
Output is the following. Note how each counter is independent, though using the same
increment()mixin to iterate.It becomes vital if one wants to maintain a proper count to increment at the usage point. Using the above incrementing code, note the differences here:
Output:
Versus this:
Outputting this:
Incrementing the counter in the same selector block is only possible by duplicating the selector. Consider this which will not work to iterate the count for the next call:
Output:
Versus this which is needed:
Output is duplicated css selector:
seven-phases-max commentedon Nov 26, 2013
Good point!
Yes, thanks for correction. I guess I was just more concerned of the scope ignorance rather than the call order.
scottgit commentedon Nov 26, 2013
You can use this counter in a loop, but it has a one item "lag" that would have to be accounted for. Using the code given above, this works:
EXCEPT it does output a
4class:There are various ways that could be overcome, but it is something to be aware of if one were to want to loop this way.
lukeapage commentedon Nov 30, 2013
its kind of magic.. but also not. when you call a mixin it evaluates arguments - so it always gets converted to a dimension inside the mixin, not a variable reference.
lukeapage commentedon Nov 30, 2013
is there anything left in this issue? is it a bug or feature request or can it be closed?
seven-phases-max commentedon Nov 30, 2013
Personally I think of [2] of the first post as a bug, but assuming that it's:
I guess it's OK to just close this (maybe later someone opens a dedicated issue report exactly for [2]).
seven-phases-max commentedon Dec 26, 2013
Actually there's one already: #1291, the example there is a bit different so it's not that simple to see the same problem but it is the same.
matthew-dean commentedon Dec 31, 2013
CSS is declarative. And order is important. Later things override (are evaluated after) earlier things.