Remove result variable
This commit is contained in:
parent
ffafafa239
commit
4abd738f6b
2 changed files with 8 additions and 7 deletions
|
|
@ -7,9 +7,10 @@ Nimrod supports three different types of variables, `let`, `var`, and `const`. A
|
|||
|
||||
``` nimrod
|
||||
proc getAlphabet(): string =
|
||||
result = ""
|
||||
var accm = ""
|
||||
for letter in 'a'..'z':
|
||||
result.add(letter)
|
||||
accm.add(letter)
|
||||
return accm
|
||||
|
||||
# Computed at compilation time
|
||||
const alphabet = getAlphabet()
|
||||
|
|
|
|||
|
|
@ -3,13 +3,13 @@ title: 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
|
||||
proc `**`(number, power: int): int =
|
||||
result = 1
|
||||
for i in 1..power:
|
||||
result *= number
|
||||
proc getAlphabet(): string =
|
||||
result = ""
|
||||
for letter in 'a'..'z':
|
||||
result.add(letter)
|
||||
```
|
||||
|
||||
A possible gotcha is declaring a new variable called `result` and expecting it to have the same semantics.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue