Positional arguments are lovely with two inputs and miserable with five.
makeUser "yurii" true 1000 false is a puzzle even for the person who wrote it,
and you get to solve it again every time you read the line.
So a function can unpack an attribute set instead, naming the inputs it wants straight out of it:
The inputs here have names, so their order no longer matters. toString turns
the number into text. We’ll return to builtins a bit later.
The names in the function pattern must match the names in the set you hand it.
Send the parcel to the hallway and get "parcel → hallway".
? gives a named input a default. If the caller leaves it out, the default
wins.
By default a named-input function is strict: hand it a name it did not ask for
and it refuses. ... says “take the names I asked for and allow the rest.”
Which is the everyday case, because the set you get handed is very often not one you wrote. Here the order sheet has more on it than the kitchen ever asked about, and the kitchen does not get to go and edit somebody’s order.
Let the kitchen take an order sheet with extra things written on it.
The fix belongs in the function’s { dish, servings ? 2 } part.
That is the whole reason ... exists. If somebody else assembles the set
you get called with, you have to say out loud whether you mind what else is in
it.
A function can build a configuration, not just a number or string. Two small named inputs go in, and a whole structured value comes out.
Name this machine round-table and give it a desktop.
Notice how you never touched makeMachine definition. Everything you changed was in the
call, which is exactly what this pattern buys you. The shape gets written once,
and the choices get made somewhere else entirely. It is one of the most common
shapes in Nix, and in the following lessons you will meet it holding a whole operating
system.
Functions do not evaluate an input unless they use it.
The broken calculation was passed in, but anything was never needed. Nix left
it sleeping.
Play around with this one. Add an input, give it a default, or make the returned set completely ridiculous. Every piece is one you already know.
{ item, room }:takes one attribute set and names the inputs it needs.servings ? 2supplies a default when the caller leavesservingsout....deliberately allows names the function does not use.- Functions can return anything, including an entire configuration.
- Lazy evaluation leaves unused inputs asleep.
You can now read the function header on packages, shells, and NixOS modules without treating it as a spell.


Share your thoughts