Skip to content

Commit 4966b52

Browse files
authored
Keyword arguments (#4)
1 parent fc637fe commit 4966b52

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

fantasy-docs.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Two or more Solid2ds can be:
7878

7979
_Solid3ds_ can be simple shapes like spheres and cubes, or a combination of them (a sphere on top of a cube).
8080

81-
Previously every type we've examined has had geometric properties, like length or height. But Solid3d is different, because it also has a _material_. Materials (for example, aluminium or plastic) let KCL analyze your model and report back important data like its weight or tensile strength. We'll discuss materials more below.
81+
Every type we've previously examined had geometric properties, like length or height. But Solid3d is different, because it also has a _material_. Materials (for example, aluminium or plastic) let KittyCAD analyze your model and report important data like its weight or tensile strength. We'll discuss materials more below.
8282

8383
Create a Solid3d by:
8484
- Extruding a 2D shape into the third dimension (along a straight line, or any path)
@@ -249,7 +249,7 @@ A KCL program is made up of _functions_. A function has a name, parameters, and
249249
/// A can for our line of *awesome* new [baked beans](https://example.com/beans).
250250
can_of_beans = (radius: Distance, height: Distance -> Solid3d) =>
251251
circle(radius)
252-
|> extrude_closed(material.aluminium, height)
252+
|> extrude_closed(height)
253253
```
254254

255255
Let's break this down line-by-line.
@@ -262,7 +262,7 @@ You could write this same function in a different way without the `|>` operator:
262262

263263
```kcl
264264
can_of_beans = (radius: Distance, height: Distance -> Solid3d) =>
265-
extrude_closed(material.aluminium, height, circle(radius))
265+
extrude_closed(height, circle(radius))
266266
```
267267

268268
But we generally find the `|>` operator makes your KCL functions easier to read.
@@ -272,7 +272,7 @@ In this example function, we specified the types of both input parameters and th
272272
```kcl
273273
can_of_beans = (radius, height) =>
274274
circle(radius)
275-
|> extrude_closed(material.aluminium, height)
275+
|> extrude_closed(height)
276276
```
277277

278278
Here, the KCL compiler:
@@ -370,6 +370,30 @@ doubleAllDistances = (distances: List Distance -> List Distance) =>
370370
List.map((x: Distance -> Distance) => x * 2, distances)
371371
```
372372

373+
#### Keyword arguments
374+
375+
All the functions you saw previously had a list of required arguments. Users will look at the function definition, read every parameter from first to last, and pass in the right values as arguments.
376+
377+
These are called _positional_ or _required_ arguments. But KCL also supports _keyword_ arguments. Like this:
378+
379+
```kcl
380+
sphere = (radius: Distance, material: Material = Aluminium.ISO5052) => ...
381+
```
382+
383+
Here, the `material` parameter is a _keyword parameter_. It's optional, so if the caller doesn't provide it, it takes a default value, in this case the `Aluminium.ISO5052` alloy from the KCL standard library.
384+
385+
You pass keyword arguments like this:
386+
387+
```kcl
388+
/// Using a keyword argument.
389+
sphere(Distance::metre(1), material = Plastic.ISO1234)
390+
391+
/// Or, don't use a keyword argument and rely on the default.
392+
sphere(Distance::metre(1))
393+
```
394+
395+
Keyword arguments help keep your KCL programs readable, and allows us to add new features to the standard library in a backwards-compatible way. Suppose that KittyCAD releases KCL 1.4, which adds a new positional argument to a standard library function `sphere`. Any programs using the definition of `sphere` from KCL 1.3 would stop compiling when you upgrade to 1.4 (because they're missing a parameter to `sphere`). But if we add the new parameter as a _keyword parameter_, your existing programs will keep working -- they'll just use the default value for that parameter.
396+
373397
### KCL files
374398

375399
KCL files are just a collection of KCL functions (including constants).

0 commit comments

Comments
 (0)