瀏覽代碼

SVI Add ch04

SVI 1 月之前
父節點
當前提交
af006a501d
共有 2 個文件被更改,包括 36 次插入24 次删除
  1. 35 24
      doc/part01/ch05.md
  2. 1 0
      doc/part01/main.md

+ 35 - 24
doc/part01/ch05.md

@@ -1,30 +1,41 @@
-# Chapter 5. Parameter passing and
-mutability
+# Chapter 5. Parameter passing and mutability
+
 Every parameter in Nim is immutable unless it is declared as a var parameter.
+
 This means that the following code does not compile:
- proc resetPointsToOrigin(points: seq[Point]) =
-    for i in 0 ..< points.len:       1
-      points[i] = Point(x: 0, y: 0) 2
- 1 We iterate over every index of points via an iterator that uses an operator
-    symbol ..<. The ..< symbol indicates that the upper bound is exclusive
-    which is exactly what we need since the indexing starts at 0.
- 2 We then try to mutate points[i] and set its new value to the point (x: 0,
-    y: 0). But the compiler rejects this statement!
-The compiler rejects the code because points is a parameter that can only be
-used for read accesses. This restriction helps us to write code that is easier to
-understand and scales better to larger programs and at the same time it
-helps the compiler to produce better machine code.
+
+```nim
+proc resetPointsToOrigin(points: seq[Point]) =
+    for i in 0 ..< points.len:      # 1
+      points[i] = Point(x: 0, y: 0) # 2
+```
+
+- 1 We iterate over every index of points via an iterator that uses an operator symbol ..<. The ..< symbol indicates that the upper bound is exclusive  which is exactly what we need since the indexing starts at 0.
+- 2 We then try to mutate points[i] and set its new value to the point (x: 0, y: 0). But the compiler rejects this statement!
+
+The compiler rejects the code because points is a parameter that can only be used for read accesses. This restriction helps us to write code that is easier to understand and scales better to larger programs and at the same time it helps the compiler to produce better machine code.
+
 In order to be allowed to mutate points we need a var parameter:
- proc resetPointsToOrigin(points: var seq[Point]) = 1
+
+```nim
+proc resetPointsToOrigin(points: var seq[Point]) = # 1
     for i in 0 ..< points.len:
-      points[i] = Point(x: 0, y: 0) 2
- 1 The points parameter is a var seq
- 2 so the mutation is allowed.
-                                                                              29
+      points[i] = Point(x: 0, y: 0)                # 2
+```
+
+- 1 The points parameter is a var seq
+- 2 so the mutation is allowed.
+
 If we now try to call resetPointsToOrigin with a seq constructor the compiler
 once again rejects our code:
-  resetPointsToOrigin @[Point(x: 2, y: 4)]
-The reason is that a sequence constructed via @[] is not mutable. A variable is
-mutable, so the following is valid:
-  var points = @[Point(x: 2, y: 4)]
-  resetPointsToOrigin points
+
+```nim
+resetPointsToOrigin @[Point(x: 2, y: 4)]
+```
+
+The reason is that a sequence constructed via @[] is not mutable. A variable is mutable, so the following is valid:
+
+```nim
+var points = @[Point(x: 2, y: 4)]
+resetPointsToOrigin points
+```

+ 1 - 0
doc/part01/main.md

@@ -4,3 +4,4 @@
 - [Глава 2](./ch02.md)
 - [Глава 3](./ch03.md)
 - [Глава 4](./ch04.md)
+- [Глава 5](./ch05.md)