-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Imperative LESS (a.k.a. recursive variable definition is actually possible) #1678
Comments
Regarding your final exampleWe can see how order is important in this case of LESS calculations. In your example, Regarding your counter ideaYou actually don't even need the extra
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
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:
|
Good point!
Yes, thanks for correction. I guess I was just more concerned of the scope ignorance rather than the call order. |
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
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. |
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. |
is there anything left in this issue? is it a bug or feature request or can it be closed? |
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]). |
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. |
CSS is declarative. And order is important. Later things override (are evaluated after) earlier things. |
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:Result:
As far as I understand it's even two issues (or at least "side effects") abused here:
.counter-redefine
called inside.counter
is the one expanded to the global namespace by initial.counter-wrapper
call in the global scope and not the one expanded by the next.counter-wrapper
local scope call (the latter is not visible yet, see Use Dynamically Generated Classes as Mixins #1399 (comment) second example)..counter-wrapper
call does not actually define new or modify old@counter
variable so LESS does not detect anyRecursive variable definition for @counter
at the.counter-wrapper(...)
stage, what.counter-wrapper
does modify is the@new
argument variable which seems to behave somewhat like a "singleton" variable regardless of the.counter-wrapper
call scope. So next.counter()->.counter-redefine()
call defines new@counter
variable initialized with the previous@new
argument 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:
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:
I.e. the
b
scope knows nothing of thea
scope, nevertheless it still can use the foreign scope value.The text was updated successfully, but these errors were encountered: