Problem with 0-N arguments when use pattern-matching or guard expressions
.mix(@type; ...) {}
.mix(bg; @color, @width) {
background: @color;
width: @width;
}
.mix(color; @color, @fontsize) {
color: @color;
font-size: @fontsize;
}
.el {
.mix(bg; blue, 10px);
}
Compile error SyntaxError: variable @color is undefined
Then i rewrite this with guard expressions:
.mix(@type...) when (@type = bg) {
.mix(@type, @color, @width) {
background: @color;
width: @width;
}
}
.mix(@type...) when (@type = color) {
.mix(@type, @color, @fontsize) {
color: @color;
font-size: @fontsize;
}
}
After compile i have empty file. Why? It's bug or my mistake?
Is there any special reason to mix ; and , in mixin's parameters declaration? Would not the following just do what you need?:
.mix(@type, ...) {}
.mix(bg, @color, @width) {
background: @color;
width: @width;
}
.mix(color, @color, @fontsize) {
color: @color;
font-size: @fontsize;
}
.el {
.mix(bg, blue, 10px);
}
Yes, mix(bg; blue, 10px); syntax is correct (this invokes the .mix with two parameters: bg and blue, 10px) but mixing both , and ; in a mixin definition seems to be sort of undefined, at least by now (as far as I can guess).
Your second snippet produces no output because you only define those nested mixins inside the outer .mix but you don't invoke/expand them (i.e. parametric mixins should be called explicitly).
It turns out that now we don't have a chance create N arguments in pattern-matching or guard expressions in LESS?
Well, I'm not quite sure I understand why do you think so... If you would explain what was the exact purpose of mixing both ; and , in the mixin definiton (have you tried the code I posted, doesn't it output what you need?).
E.g. it is possible to write:
.mix(bg, @color, @width) {/* case 1 */}
.mix(color, @color, @fontsize) {/* case 2 */}
.mix(something-else, @color, @arg3, @arg4) {/* case 3 */}
.mix(even-more-else, another, @arg3, @arg4, @arg5, @more...) {/* case 5 */}
.mix(less, @arg1) {/* case 6 */}
.mix(it-could-be-only-one) {/* case 7 */}
// etc. etc.
And this all will match as expected.
Thanks much! Problem is in my head, my inattention. All is very simple, unlike I thought. Soon publish cool mixin =)
Thanks I was halfway through a long reply.