Slide 30 of 74

Named arguments

type Colony {
	name:   Str
	apiary: Str
	frames: Int
}

func banner(text: Str, width: Int, fill: Str): Str {
	return fill + " " + text + " " + fill + " ({width})"
}

func noisy(label: Str): Str {
	echo "working out {label}"
	return label
}

proc main(): void {
	echo banner(width: 40, text: "Hive", fill: "~")   // any order, once all are named
	echo banner("Hive", fill: "~", width: 40)          // the unnamed one fills `text`

	c := Colony(name: "north", apiary: "orchard", frames: 11)
	echo c.frames

	echo join(sep: noisy(" / "), vector: [noisy("a"), "b"])   // " / " is worked out first
	echo hive.math.clamp(high: 1.0, value: 3.0, low: 0.0)      // 1
}

Every call accepts its arguments by name: a func, a proc, a query, a type's constructor, a builtin and every hive.* call. A named argument may appear anywhere in the list; only the unnamed ones have to be in order, and they fill whichever parameters the names did not claim. A name has to exist and may not repeat — a misspelt one is refused with the names there are — and once a call names anything it has to cover the whole parameter list, so nothing can read as a default that quietly went missing. Arguments are worked out in the order they are written, whatever order the parameters come in.

A builtin's names are the ones on its slide, and a library call's the ones in its signature: join(sep: ", ", vector: names), hive.math.clamp(value: x, low: 0.0, high: 1.0). A function value is the one call with nothing to name — its type lists what it takes, not what each thing is called.

The tour you are reading is written this way itself: each slide is a Slide(title: …, titlePt: …, explanation: …) rather than a row of strings whose meaning depends on counting them.