One kind of list

Discs, one of the pieces in the table below. Each disc is a record in a list, hatched at its own angle.
I asked a simple question: how do lists work in Nilo? The answer took too long, which told me something was wrong.
There were two ways to hold many things. An array held many of one type: points, numbers, colours. A list held
rows with named fields, such as a reed with a root, a height and a tip colour. They had different rules. A list
could only be filled at the start. A row could not leave its list: I could not keep one in a name, hand it to a
function, or get one back.
Two words for nearly the same idea is one too many. For a language whose whole aim is fewer ideas to state, it was a fault.
A kind
Now there is one. A kind says what one thing looks like. Then it is kept in an ordinary list, like anything
else.
kind reed { root: vec, h: num, tip: color }
let reeds = reed[]
reeds.add(reed(root: vec(x, 2100), h: 900, tip: tips[2]))
for r in reeds { circle(r.root, 9); fill(r.tip) }A reed is now a real thing. I can pick one out, pass it to a function, and have a function give one back:
fn nearest(q: vec): lump { ... return lumps[best] }
let l = nearest(q)Before, that function had to return a position in the list, and every caller had to go and look it up.
The old word
Writing list is now refused. The compiler does not just say no. It writes out what I should have typed, using my
own names:
there is no list: write kind reed { root: vec, tip: color } and let reeds = reed[]This is the second word to be retired this way, and it is how I want every change to the language to go. If I come back to an old piece in a year, it tells me how to bring it up to date.
One word had to move to make room. A named random with its own recipe used to be called a kind of random. It is now a random maker.
What it cost
I expected to pay for this in size. A list was laid out in a compact way in the shipped file, and I assumed that was the reason it was a separate thing.
So before anything was changed, it was measured. A rough version was built in a scratch copy and every existing piece was rewritten with it.
| Piece | Before | After |
|---|---|---|
| drift | 896 | 896 |
| reeds | 991 | 991 |
| discs | 974 | 974 |
| daily | 14,779 | 14,779 |
| One real layer | 10,180 | 10,180 |
Byte for byte the same. The compiler keeps the compact layout underneath; only what I write has changed. The obvious way of doing it, where each thing becomes a plain object in the shipped file, was measured too, and came out 5 to 14 percent bigger. That one was thrown away.
The cost is in the source. Each piece is one or two lines longer, because the shape and the list are now two statements instead of one.
That table is the like for like rewrite. After it, two pieces were tidied to use what a kind allows: handing a whole record to a function, instead of its position in the list. The daily piece came out 1 byte bigger, at 14,780, and the layer 3 bytes smaller, at 10,177. Both draw exactly what they drew before.
There is one small loss in speed. In the faster of the two build modes, reeds draws a frame 6 to 13 percent slower. That is about two millionths of a second. I am noting it because it is real, not because it matters.
Found on the way
Making one rule out of two turned up faults that had been hiding in the gap between them.
- A plain list could be added to while a loop was running over it, and the loop would never end. Only the old
listwas protected. Now everything is. - A piece that took values from the page it runs in could silently lose its lookup tables in the build. Nothing had shipped that way, but nothing would have warned me either.
- The start of a piece and its animation frame are kept apart, so nothing leaks from one frame to the next. There was a way round that through a function. It is closed.
The checks went from 470 to 637.
Curves, tidied
The same round fixed the three things I did not like about curves in entry 5.
Smoothing is part of the mark now. I give it the points and it draws a smooth line through them:
curve(outline, smooth: 0.9, closed: true, fill: ink)Closing a curve is something I say, with closed: true. Before, a curve was closed if it happened to end where
it began, which is the kind of rule that works until the day it does not.
And a group can be cut to a curve directly:
place(opacity: 0.6, clip: curve(outline, closed: true)) { ... }The hills piece was rewritten with all three. It came out 42 bytes smaller and one step clearer.
What is not right yet
- A kind cannot hold a list or another kind. A reed cannot yet carry its own path.
- A library cannot define a kind.
- A few slips are not caught: taking something out of a list while looping over it, and asking for a slice past the end.
- Two pieces could now be written more simply than they are. They were left alone, because the simpler versions change the shipped file by a byte or three, and I wanted this change to move nothing.
- Curves still draw on the SVG target only.
This was the last change to the language before the compiler is rewritten.
The piece at the top
The image at the top comes from this token, built for the svg target: 0x9e0df50fed297d48b57cf870383dbd4c5e7fc88f68b54047507b87be9f4e2635
// discs: a static plate. Packed discs, each hatched at its own angle.
plate(1920, 2400)
random layout, mark
let paper = #f3efe6
let ink = #1b1b1f
kind disc { pos: vec, r: num, ang: num, gap: num }
let discs = disc[]
once {
for i in 0 ..< 4000 {
let p = vec(layout.float(160 ..< 1760), layout.float(160 ..< 2240))
let r = layout.float(40 ..< 220)
var ok = discs.len < 90
for d in discs {
if len(d.pos - p) < d.r + r + 12 {
ok = false
break
}
}
if ok {
discs.add(disc(pos: p, r: r, ang: mark.float(0 ..< PI), gap: mark.float(5 ..< 14)))
}
}
background(paper)
for d in discs {
let u = polar(d.ang, 1)
let v = perp(u)
let m = floor(d.r / d.gap)
for k in -m ..= m {
let off = k * d.gap
let half = sqrt(d.r * d.r - off * off)
let mid = d.pos + v * off
line(mid - u * half, mid + u * half)
}
}
stroke(ink, 1.2)
for d in discs {
circle(d.pos, d.r)
}
stroke(ink, 2.5)
}