Skip to content

Commit c41bce9

Browse files
committed
Better shorthand for units
1 parent 55609db commit c41bce9

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

fantasy-docs.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ A measurement type (e.g. `Force` or `Distance`) may have a lot of different cons
138138
1. A predefined unit, e.g.
139139
a. `Force.Newton(1)`
140140
b. `Force.Pound(0.224809)`
141-
c. `Distance.Metre(1)`
141+
c. `Distance.Meter(1)`
142142
d. `Distance.Yard(0.9144)`
143143
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)
147147

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.
149149

150150
#### Operations on measurements
151151

@@ -296,16 +296,21 @@ This is an expression which evaluates `can_of_beans` with its two input paramete
296296
Some units have aliases, so you could also write
297297

298298
```kcl
299-
can_of_beans(Cm(10), Ft(1))
299+
can_of_beans(10.cm, 1.ft)
300300
```
301301

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+
302307
See the [docs](units) for all units and aliases.
303308

304309
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:
305310

306311
```kcl
307312
let
308-
can_radius = Cm(10)
313+
can_radius = 10.cm
309314
can_height = can_radius * 5
310315
in can_of_beans(can_radius, can_height)
311316
```
@@ -319,21 +324,21 @@ KCL doesn't have any mutation or changes, so there aren't any variables. Files c
319324
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.
320325

321326
```kcl
322-
my_can = can_of_beans(Cm(10), Ft(1))
327+
my_can = can_of_beans(10.cm, 1.ft)
323328
```
324329

325330
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:
326331

327332
```kcl
328-
my_can: Solid3d = can_of_beans(Cm(10), Ft(1))
333+
my_can: Solid3d = can_of_beans(10.cm, 1.ft)
329334
```
330335

331336
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.
332337

333338
Without the syntactic sugar, `my_can` could be declared like this:
334339

335340
```kcl
336-
my_can = (-> Solid3d) => can_of_beans(Cm(10), Ft(1))
341+
my_can = (-> Solid3d) => can_of_beans(10.cm, 1.ft)
337342
```
338343

339344
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:
386391

387392
```kcl
388393
/// Using a keyword argument.
389-
sphere(Distance::metre(1), material = Plastic.ISO1234)
394+
sphere(1.m, material = Plastic.ISO1234)
390395
391396
/// Or, don't use a keyword argument and rely on the default.
392-
sphere(Distance::metre(1))
397+
sphere(1.m)
393398
```
394399

395400
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

Comments
 (0)