SVI 1 mese fa
parent
commit
d12163b7a7
1 ha cambiato i file con 11 aggiunte e 12 eliminazioni
  1. 11 12
      doc/part01/ch05.md

+ 11 - 12
doc/part01/ch05.md

@@ -1,8 +1,8 @@
-# Chapter 5. Parameter passing and mutability
+# Глава 5. Передача параметров и изменяемость
 
-Every parameter in Nim is immutable unless it is declared as a var parameter.
+Каждый параметр в **Nim** является неизменяемым, если только он не объявлен как параметр `var`.
 
-This means that the following code does not compile:
+Это означает, что следующий код не компилируется:
 
 ```nim
 proc resetPointsToOrigin(points: seq[Point]) =
@@ -10,12 +10,12 @@ proc resetPointsToOrigin(points: seq[Point]) =
       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!
+- 1 Код перебирает все индексы точек с помощью итератора, использующего операторный символ `..<`. Символ `..<` указывает на то, что верхняя граница является исключительной, а это именно то, что нам нужно, поскольку индексация начинается с 0.
+- 2 Затем код пытается изменить `points[i]` и присвоить ему новое значение — `Point(x: 0, y: 0)`. Но компилятор отклоняет это выражение!
 
-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.
+Компилятор отклоняет этот код, поскольку points — это параметр, который можно использовать _только для чтения_. Это ограничение помогает нам писать более понятный код, который лучше масштабируется для больших программ, и в то же время помогает компилятору генерировать более качественный машинный код.
 
-In order to be allowed to mutate points we need a var parameter:
+Чтобы иметь возможность изменять точки, нам нужен параметр `var`:
 
 ```nim
 proc resetPointsToOrigin(points: var seq[Point]) = # 1
@@ -23,17 +23,16 @@ proc resetPointsToOrigin(points: var seq[Point]) = # 1
       points[i] = Point(x: 0, y: 0)                # 2
 ```
 
-- 1 The points parameter is a var seq
-- 2 so the mutation is allowed.
+- 1 Параметр `points` — это изменяемая последовательность переменных
+- 2 значит, изменение допустимо.
 
-If we now try to call resetPointsToOrigin with a seq constructor the compiler
-once again rejects our code:
+Если теперь попробовать вызвать `resetPointsToOrigin` с помощью конструктора `seq`, компилятор снова отклонит наш код:
 
 ```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:
+Причина в том, что последовательность, созданная с помощью `@[]`, неизменяема. Переменная с параметром `var` может быть изменена, поэтому следующий код допустим:
 
 ```nim
 var points = @[Point(x: 2, y: 4)]