Complex Value Array in Stylus

Stylus is an awesome CSS pre-processor, which provides much more concise syntax and more powerful feature than its competitors, such as LESS or SCSS.

But now, with more and more features added into Stylus, it seems its syntax become over-weighted. Pitfall come up.

I wish to declare an array of values for box-shadow property. And I can reference them with index:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
drop-shadows = [
0 2px 10px 0 rgba(0, 0, 0, 0.16),
0 6px 20px 0 rgba(0, 0, 0, 0.19),
0 17px 50px 0 rgba(0, 0, 0, 0.19),
0 25px 55px 0 rgba(0, 0, 0, 0.21),
0 40px 77px 0 rgba(0, 0, 0, 0.22)
]
drop-shadow(n)
box-shadow shadows[n]
for i in (1..5)
.drop-shadow-{i}
drop-shadow(i)

And expect it generates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.drop-shadow-1 {
box-shadow: 0 2px 10px 0 rgba(0, 0, 0, 0.16);
}
.drop-shadow-2 {
box-shadow: 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.drop-shadow-3 {
box-shadow: 0 17px 50px 0 rgba(0, 0, 0, 0.19);
}
.drop-shadow-4 {
box-shadow: 0 25px 55px 0 rgba(0, 0, 0, 0.21);
}
.drop-shadow-5 {
box-shadow: 0 40px 77px 0 rgba(0, 0, 0, 0.22;
}

But I found there is not such thing called Array in Stylus!!!!
There is only Hash, and Hash doesn’t accept number as key!
So finally, I come up something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
drop-shadows = {
'1': 0 2px 10px 0 rgba(0, 0, 0, 0.16),
'2': 0 6px 20px 0 rgba(0, 0, 0, 0.19),
'3': 0 17px 50px 0 rgba(0, 0, 0, 0.19),
'4': 0 25px 55px 0 rgba(0, 0, 0, 0.21),
'5': 0 40px 77px 0 rgba(0, 0, 0, 0.22)
}
drop-shadow(n)
box-shadow shadows[n+'']
for i in (1..5)
.drop-shadow-{i}
drop-shadow(i)

In this piece of code, there are a bunch of pitfalls:

  1. Hash doesn’t accept number as key. So 1: 0 2px 10px 0 rgba(0, 0, 0, 0.16) cause compile error.
  2. '1' != 1, so drop-shadows[1] returns null
  3. There is no type conversion function in Stylus, use the same trick as JavaScript. ''+n convert n into string.

Just found Stylus provides something called List, which is pretty much similar to what array in other languages, except the syntax.

1
2
3
4
5
6
7
8
9
10
11
12
13
drop-shadows = 0 2px 10px 0 rgba(0, 0, 0, 0.16),
0 6px 20px 0 rgba(0, 0, 0, 0.19),
0 17px 50px 0 rgba(0, 0, 0, 0.19),
0 25px 55px 0 rgba(0, 0, 0, 0.21),
0 40px 77px 0 rgba(0, 0, 0, 0.22)
drop-shadow(n)
box-shadow shadows[n]
for i in (1..5)
.drop-shadow-{i}
drop-shadow(i)

So no brackets or parentesis needed.

Negative Index in Coffee-Script

Coffee Script borrowed quite a lot syntax patterns from both Ruby and Python, especially from Ruby.
So people, like me, usually tends to write coffee-script in ruby way.

In ruby, we can retrieve the element in an array in reversed order by using a negative index, which means array[-1] returns the last element in the array. This grammar sugar is really convenient and powerful, so we can omit the code like this array[array.length - 1].

But for some reason, coffee-script doesn’t inherit this syntax. To me, it is weird. But after analyze this problem in detail, I found the reason.
Coffee script announce it has a golden rule: “coffee-script is just javascript”. So all the coffee script must be able to compiled into javascript.

Let’s try to analyze how the coffee script is compiled into javascript:

Coffee Script
1
2
3
array = [1..10]
second = array[1]
last = array[-1] // Psudocode

Obviously, the previous code should be compiled as following:

JavaScript
1
2
3
4
var array, last, second;
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
second = array[1];
last = array[array.length - 1];

The negative index should be processed specially, so we should check the index is negative or not while compiling. This translation seems easy but actually not, since we can and usually use variable as the index.

Variable as index
1
2
3
4
5
array = [1..10]
index = 1
second = array[index]
index = -index
last = array[index]

In the previous code, because we use the variable as index, which cannot be verified in compile-time, which means we need to compile the array reference code as following:

Compile Result
1
2
3
4
5
6
var array, index, last, second;
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
index = 1;
second = index >=0 ? array[index] : array[array.length + index];
index = -index;
last = index >=0 ? array[index] : array[array.length + index];

So every time we reference the array, we need to check whether the index is negative or not. This approach absolutely hurts the performance a lot, which in basically unacceptable.
So that’s why coffee-script doesn’t support the negative index.