Remove result variable

This commit is contained in:
flaviut 2014-07-17 13:31:19 -04:00
commit 4abd738f6b
2 changed files with 8 additions and 7 deletions

View file

@ -7,9 +7,10 @@ Nimrod supports three different types of variables, `let`, `var`, and `const`. A
``` nimrod ``` nimrod
proc getAlphabet(): string = proc getAlphabet(): string =
result = "" var accm = ""
for letter in 'a'..'z': for letter in 'a'..'z':
result.add(letter) accm.add(letter)
return accm
# Computed at compilation time # Computed at compilation time
const alphabet = getAlphabet() const alphabet = getAlphabet()

View file

@ -3,13 +3,13 @@ title: Result
--- ---
# Result # Result
The `result` variable is a special variable that serves as an implicit return variable, which exists because the control flow semantics of the `return` statement are rarely needed. The result variable is initialized in the standard way, as if it was declared with `var result: T`. The `result` variable is a special variable that serves as an implicit return variable, which exists because the control flow semantics of the `return` statement are rarely needed. The result variable is initialized in the standard way, as if it was declared with `var result: ReturnType`. For example, the `getAlphabet()` function could be rewritten more concisely as
``` nimrod ``` nimrod
proc `**`(number, power: int): int = proc getAlphabet(): string =
result = 1 result = ""
for i in 1..power: for letter in 'a'..'z':
result *= number result.add(letter)
``` ```
A possible gotcha is declaring a new variable called `result` and expecting it to have the same semantics. A possible gotcha is declaring a new variable called `result` and expecting it to have the same semantics.