less.js icon indicating copy to clipboard operation
less.js copied to clipboard

Problem with 0-N arguments when use pattern-matching or guard expressions

Open Pestov opened this issue 13 years ago • 5 comments

.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?

Pestov avatar Oct 13 '13 17:10 Pestov

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).

seven-phases-max avatar Oct 13 '13 20:10 seven-phases-max

It turns out that now we don't have a chance create N arguments in pattern-matching or guard expressions in LESS?

Pestov avatar Oct 13 '13 20:10 Pestov

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.

seven-phases-max avatar Oct 13 '13 21:10 seven-phases-max

Thanks much! Problem is in my head, my inattention. All is very simple, unlike I thought. Soon publish cool mixin =)

Pestov avatar Oct 13 '13 22:10 Pestov

Thanks I was halfway through a long reply.

lukeapage avatar Oct 13 '13 22:10 lukeapage