较少的列表作为混合参数 [英] Less lists as a mixin argument(s)

查看:41
本文介绍了较少的列表作为混合参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有这个混搭:

.loop-strings("A, B, C", "1, 2, 3", "X, Y, Z";);

实现方式如下:

.loop-strings(@list, @index: 1) when (isstring(extract(@list, @index))) {
    @currentMember: extract(@list, @index);

    .do-something-with(@currentMember);

    .loop-strings(@list, (@index + 1)); /* loop the next member */
}

.do-something-with(...) {
    @args1 : e(@arguments); 
    @args2 : A, B, C;
    args1: @args1;
    args2: @args2;
    extract-args1-2: extract(@args1, 2);
    extract-args2-2: extract(@args2, 2);
}

结果:

args1: A, B, C;
extract-args1-2: extract(A, B, C, 2);
args1: 1, 2, 3;
extract-args1-2: extract(1, 2, 3, 2);
args1: X, Y, Z;
args2: A, B, C;
extract-args1-2: extract(X, Y, Z, 2);
extract-args2-2: B;

这些接缝是@foo:e("A, B, C");@foo:~"A, B, C";@foo:A, B, C;

我似乎不能使用extract(@foo, 2);,除非将其定义为对象列表.

I seems i can't use extract(@foo, 2); unless it is defined as an object list.

是否有一种方法可以将隐匿的字符串转换为对象列表

Is there a way to convert an esacaped string to an object list

推荐答案

可能是e("A, B, C")~"A, B, C"A, B, C

是的,e("A, B, C")~"A, B, C"都创建了所谓的匿名值"类型,该类型永远不会被视为有意义的类型(它不是列表,不是数字,甚至不是字符串).基本上,逃避的值只是不要碰我"或我知道我在做什么!"之类的东西.东西,它们只是按原样"输出,并且编译器从不尝试了解其中的内容.基本上,这正是转义的值的确切含义:打印"出编译器无法理解的内容.

Yes, both e("A, B, C") and ~"A, B, C" create so-called "anonymous value" type which is never considered as a meaningful type (it's not a list, not a number, not even a string). Basically an escaped values are just something like "Don't touch me" or "I know what I'm doing!" stuff, they are just being output "as is" and the compiler never tries to understand what's inside. This is basically what exactly the escaped values are for: "print" out something the compiler can't understand.

通常请注意,您可以在列表中同时使用逗号和空格作为值定界符.例如,您可以使用.loop-strings(A B C, 1 2 3, X Y Z;);(二维列表作为单个参数,因此使用多参数mixin,甚至可以在一行中获得一个树形列表).您是否有任何特殊原因需要使用带引号和/或转义的值?例如,您可以将其编写为:

In general notice that you can use both comma and space as the value delimiter in a list. For example you can use .loop-strings(A B C, 1 2 3, X Y Z;); (two-dimensional list as a single parameter, so with a multi-argument mixin you even can get a tree-dimensional list in one line). Is there any particular reason you need to use quoted and/or escaped values? For example you could write it just as:

test {
    .loop-lists(A, B, C; 1, 2, 3; X, Y, Z);
}

.loop-lists(@lists...) {
    .loop(length(@lists));
    .loop(@i) when (@i > 0) {
        .loop((@i - 1));
        .do-something-with(extract(@lists, @i));
    }
}

.do-something-with(@list) {
    v1: extract(@list, 1);
    v2: extract(@list, 2);
    v3: extract(@list, 3);
}

---

提取(A,B,C,2);

extract(A, B, C, 2);

目前这是错误的extract语法,extract仅接受两个参数,因此您可以将其写为:

For the moment this is incorrect extract syntax, extract accepts only two parameters so you could write this as:

extract(A B C, 2);

或作为:

@list: A, B, C;
extract(@list, 2);

---

这是一个带有几个其他通用提示的示例:

---

Here's an example with couple of additional generic hints:

test {
    .do-something(A B C, 1 2 3, X Y Z; foo bar, baz; banana);
}

.do-something(@p1, @p2, @p3) {
    args1: @arguments;                                     // 3D list
    args2: extract(@arguments, 1);                         // 2D list: A B C, 1 2 3, X Y Z
    args3: extract(extract(@arguments, 1), 1);             // 1D list: A B C
    args4: extract(extract(extract(@arguments, 1), 1), 1); // single value: A

    p1- :   @p1;               // A B C, 1 2 3, X Y Z
    p1-1:   extract(@p1, 1);   // A B C
    p1-3:   extract(@p1, 3);   // X Y Z

   @p2-1:   extract(@p2, 1);   // foo bar
    p2-1:   @p2-1;             // foo bar
    p2-1-2: extract(@p2-1, 2); // bar
    p2-2:   extract(@p2, 2);   // baz

    p3- :   @p3;               // banana
    p3-1:   extract(@p3, 1);   // banana
    // etc.

    quoted-p2: "@{p2}"; // if you need a quoted string do it in reverse (i.e. non-quoted list to a quoted string)
}

这篇关于较少的列表作为混合参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆