Types, patterns and valuesSlide 35 of 86
35x:Types, patterns and values
Named arguments
Grammar: call-suffix, args, arg
call-suffixSpecification §2.6
call-suffix = "(" [ args ] ")" ;args = arg { "," arg } [ "," ] ;As the compiler reads it; §2.6 of the specification is out of date here.
arg = [ IDENT ":" ] expression ;
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; the unnamed ones keep their order and fill whichever parameters the names did not claim. The rules:
- a name has to exist and may not repeat, and a misspelt one is refused with the list of names there are;
- once a call names anything it has to supply every parameter, 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 call through a function value is the one call with nothing to name: its type lists what it takes, not what each thing is called.
This tour is written this way: each slide is a Slide(title: …, titlePt: …, explanation: …) rather than a row of strings whose meaning depends on counting them.
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
}You can edit this program and run it here.Tab indents, Escape leaves the program, and Ctrl+Enter runs it.
Output
Run a program to see what it prints here.