As a LESS-newbie, I'm puzzled by the parser's behaviour on a particular piece of code.
I'm following a course that teaches me to enter this code in my LESS-file and parse it:
.lusGuardedMixin (@index) when (@index > 0) {
@waarde: -((38*@index)-38);
.m_item@{index} {
background-position: ~"@{waarde}px" 0;
&:hover {
background-position: ~"@{waarde}px" -39px;
}
& span {
.menuspan(~"bgcolor_@{index}", ~"color_@{index}", ~"tshadow_@{index}")
}
}
.lusGuardedMixin(@index + 1);
}
.lusGuardedMixin(6) {}
.lusGuardedMixin(1);
You can ignore the ".menuspan"-part, that refers to a piece of code that is not relevant here.
Now, when running this code in WinLESS, it creates a parser error: "SYNTAR ERROR: undefined", then refers to the line where i stated:
.lusGuardedMixin(1);
I tried all kinds of things - like changing the starting number -, but keeping the LESS-documentation (and its example for recursion) in mind, I eventually changed part of the code to:
[...]
.lusGuardedMixin(@index - 1);
}
.lusGuardedMixin(0) // this is no longer explicitly necessary due to the restriction "> 0", but is still here for reference of changes I did try
.lusGuardedMixin(5);
And now it works! But I don't know why... What makes the increment (@index + 1) fail, but the decrement (@index - 1) work in this piece of code?
Note: I also managed to solve it using the original counter increment by replacing the original loop conditionwhen (@index > 0)
with when (@index > 0) and (@index < 6)
, but then I still can't see why the original example won't work.
Thanks in advance for your help :)