Prechádzať zdrojové kódy

Add 'Constant definitions' chapter

David Peter 2 rokov pred
rodič
commit
4e0ce0d5a7

+ 1 - 1
book/src/SUMMARY.md

@@ -26,7 +26,7 @@
   - [Operators and their precedence](./operators.md)
   - [Unit conversions](./unit-conversions.md)
   - [Printing, testing, debugging](./procedures.md)
-  - [Variable definitions]()
+  - [Constant definitions](./constant-definitions.md)
   - [Function definitions]()
   - [Conditionals](./conditionals.md)
 - [Advanced](./advanced.md)

+ 16 - 0
book/src/constant-definitions.md

@@ -0,0 +1,16 @@
+# Constant definitions
+
+New constants can be introduced with the `let` keyword:
+```nbt
+let pipe_radius = 1 cm
+let pipe_length = 10 m
+let Δp = 0.1 bar
+```
+
+Definitions may contain a type annotation after the identifier (`let Δp: Pressure = 0.1 bar`). This annotation will be verified by the type checker. For more complex definitions
+it can be desirable to add type annotations, as it often improves readabililty and allows
+you to catch potential errors early:
+```nbt
+let μ_water: DynamicViscosity = 1 mPa·s
+let flow_rate: Volume / Time = π × pipe_radius^4 × Δp / (8 μ_water × pipe_length)
+```

+ 2 - 2
book/src/example-numbat_syntax.md

@@ -55,9 +55,9 @@ pi/3 + pi // cos  # Same, 'arg // f' is equivalent to 'f(arg)'
                   # which makes it very useful for interactive
                   # terminals (press up-arrow, and add '// f')
 
-# 4. Variable definitions
+# 4. Constants
 
-let n = 4                          # Simple numerical variable
+let n = 4                          # Simple numerical constant
 let q1 = 2 m/s                     # Right hand side can be any expression
 let q2: Speed = 2 m/s              # With optional type annotation
 let q3: Length / Time = 2 m/s      # more complex type annotation

+ 2 - 2
examples/numbat_syntax.nbt

@@ -50,9 +50,9 @@ pi/3 + pi // cos  # Same, 'arg // f' is equivalent to 'f(arg)'
                   # which makes it very useful for interactive
                   # terminals (press up-arrow, and add '// f')
 
-# 4. Variable definitions
+# 4. Constants
 
-let n = 4                          # Simple numerical variable
+let n = 4                          # Simple numerical constant
 let q1 = 2 m/s                     # Right hand side can be any expression
 let q2: Speed = 2 m/s              # With optional type annotation
 let q3: Length / Time = 2 m/s      # more complex type annotation