You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: fantasy-docs.md
+17-12Lines changed: 17 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -138,14 +138,14 @@ A measurement type (e.g. `Force` or `Distance`) may have a lot of different cons
138
138
1. A predefined unit, e.g.
139
139
a. `Force.Newton(1)`
140
140
b. `Force.Pound(0.224809)`
141
-
c. `Distance.Metre(1)`
141
+
c. `Distance.Meter(1)`
142
142
d. `Distance.Yard(0.9144)`
143
143
2. A combination of different units, using a standard mathematical definition, e.g.
144
-
a. `Force.mdt(Mass.Kilogram(1), Distance.Metre(1), Time.Second(1))` (force = mass * distance * time)
145
-
b. `Area.rectangle(Distance.Metre(1), Distance.Metre(2))` (area = distance_x * distance_y)
146
-
c. `Area.rectangle(Distance.Metre(1), Distance.Yard(1.8288))` (same equation as above, but mixing imperial and metric)
144
+
a. `Force.mdt(Mass.Kilogram(1), Distance.Meter(1), Time.Second(1))` (force = mass * distance * time)
145
+
b. `Area.rectangle(Distance.Meter(1), Distance.Meter(2))` (area = distance_x * distance_y)
146
+
c. `Area.rectangle(Distance.Meter(1), Distance.Yard(1.8288))` (same equation as above, but mixing imperial and metric)
147
147
148
-
Both these expressions have the same _type_ (`Force`) but use different _constructors_ to give the specific value. For more about syntax, see the Syntax section below.
148
+
Both these expressions have the same _type_ (`Force`) but use different _constructors_ to give the specific value. KCL has a few shorthand notations to make units more convenient. See the Syntax section below for more.
149
149
150
150
#### Operations on measurements
151
151
@@ -296,16 +296,21 @@ This is an expression which evaluates `can_of_beans` with its two input paramete
296
296
Some units have aliases, so you could also write
297
297
298
298
```kcl
299
-
can_of_beans(Cm(10), Ft(1))
299
+
can_of_beans(10.cm, 1.ft)
300
300
```
301
301
302
+
These are all different ways of writing the same distance:
303
+
-`Distance.Meter(1)` -- fully spelled out, using normal KCL syntax.
304
+
-`1.Meter` -- using "unit suffix" notation, which is syntactical sugar (aka shorthand) for the above
305
+
-`1.m` -- very commonly-used units have a second, shorter alias.
306
+
302
307
See the [docs](units) for all units and aliases.
303
308
304
309
Every KCL function body is a single expression. If a function body gets really big, it might be hard to read in a single expression. So you can factor out certain parts into named constants, using let-in notation. Like this:
305
310
306
311
```kcl
307
312
let
308
-
can_radius = Cm(10)
313
+
can_radius = 10.cm
309
314
can_height = can_radius * 5
310
315
in can_of_beans(can_radius, can_height)
311
316
```
@@ -319,21 +324,21 @@ KCL doesn't have any mutation or changes, so there aren't any variables. Files c
319
324
Other languages have named constants and variables. KCL doesn't have variables (because the language describes unchanging geometry and physical characteristics of real-world objects). But it _does_ have named constants. Here's how you declare them.
320
325
321
326
```kcl
322
-
my_can = can_of_beans(Cm(10), Ft(1))
327
+
my_can = can_of_beans(10.cm, 1.ft)
323
328
```
324
329
325
330
This declares a named constant called `my_can`, which is the result of calling the `can_of_beans` function we defined above. KCL compiler inferred the type, but you can add a type annotation if you want to:
326
331
327
332
```kcl
328
-
my_can: Solid3d = can_of_beans(Cm(10), Ft(1))
333
+
my_can: Solid3d = can_of_beans(10.cm, 1.ft)
329
334
```
330
335
331
336
This named constant is actually just syntactic sugar for a function that takes 0 parameters. After all, functions called with the same inputs always return the same value -- they're fully deterministic. So a function with 0 parameters is just a function that always returns a constant value. Or, to simplify: it _is_ a constant value.
332
337
333
338
Without the syntactic sugar, `my_can` could be declared like this:
Note the function signature. Function signatures are always (parameters -> return type), but here we have no parameters, so the function signature just omits them.
@@ -386,10 +391,10 @@ You pass keyword arguments like this:
386
391
387
392
```kcl
388
393
/// Using a keyword argument.
389
-
sphere(Distance::metre(1), material = Plastic.ISO1234)
394
+
sphere(1.m, material = Plastic.ISO1234)
390
395
391
396
/// Or, don't use a keyword argument and rely on the default.
392
-
sphere(Distance::metre(1))
397
+
sphere(1.m)
393
398
```
394
399
395
400
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.
0 commit comments