Merge branch 'master' of https://github.com/flaviut/nimrod-by-example
This commit is contained in:
commit
f1a45ff3de
2 changed files with 25 additions and 20 deletions
|
|
@ -9,32 +9,37 @@ proc getAlphabet(): string =
|
|||
result.add(letter)
|
||||
|
||||
const abcs = getAlphabet()
|
||||
|
||||
var
|
||||
foo = "bar"
|
||||
bar = "foo"
|
||||
# oops, I need to fix the names
|
||||
let temp = foo
|
||||
foo = bar
|
||||
bar = temp
|
||||
a = "foo"
|
||||
b = 0
|
||||
|
||||
echo "abcs is ", abcs
|
||||
echo "foo is ", foo
|
||||
echo "bar is ", bar
|
||||
let
|
||||
c = "foo"
|
||||
d = 5
|
||||
|
||||
a.add "bar" # This is fine because ``a`` is mutable.
|
||||
b.inc # This is also fine.
|
||||
|
||||
c.add "bar" # This will result in an error.
|
||||
d.inc # As will this.
|
||||
```
|
||||
|
||||
```
|
||||
$ nimrod c -r ./assignment.nim
|
||||
abcs is abcdefghijklmnopqrstuvwxyz
|
||||
foo is foo
|
||||
bar is bar
|
||||
$ nimrod c --verbosity:2 ./assignment.nim
|
||||
a20.nim(19, 0) Error: for a 'var' type a variable needs to be passed
|
||||
c.add "bar" # This will result in an error.
|
||||
^
|
||||
```
|
||||
|
||||
A `const` variable is evaluated at compile-time, so if you inspect the C sources, you'll see the following line:
|
||||
Without `--verbosity:2` only the error will be shown.
|
||||
|
||||
A `const` variable's value will be evaluated at compile-time, so if you inspect the C sources, you'll see the following line:
|
||||
|
||||
``` C
|
||||
STRING_LITERAL(TMP129, "abcdefghijklmnopqrstuvwxyz", 26);
|
||||
```
|
||||
|
||||
The limitation with `const` is that it cannot interface with C because there is no compile-time foreign function interface at this time.
|
||||
The limitation with this is that procedures which are evaluated at compile-time cannot interface with C because there is no compile-time foreign function interface at this time.
|
||||
|
||||
`var` variables are the standard mutable variables, you can modify them after they've been assigned. `let` variables are also fairly standard immutable variables. This means you can assign to them at creation time and can't change them later on.
|
||||
As seen in the example, `var` variables are standard mutable variables, you can modify them after they've been assigned. `let` variables on the other hand are immutable variables. This means you can assign to them at creation time and can't change them later on. The difference between `let` and `const` is that a `const`'s expression must be evaluated at compile-time, a `let`'s expression doesn't have this requirement.
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
# Result
|
||||
|
||||
The `result` variable is a special variable that serves as an implicit return. This is useful because in most cases, the control flow semantics of the `return` statement are unneeded. `result` is initialized with the default value for a given return type, so the the following code it starts as `0`, before being initialized to `number`
|
||||
The `result` variable is a special variable that serves as an implicit return variable. This is useful because in most cases, the control flow semantics of the `return` statement are unneeded. `result` is initialized with the default value for a given return type, so the following code starts as `0`, before being initialized to `number`:
|
||||
|
||||
``` nimrod
|
||||
proc expr(number, power: int): int =
|
||||
proc `**`(number, power: int): int =
|
||||
result = number
|
||||
for i in 1..power:
|
||||
result *= number
|
||||
```
|
||||
|
||||
A common gotcha is declaring a variable called result and expecting it to have the same semantics.
|
||||
A possible gotcha is declaring a variable called result and expecting it to have the same semantics.
|
||||
|
||||
``` nimrod
|
||||
proc unexpected(): int =
|
||||
|
|
@ -17,4 +17,4 @@ proc unexpected(): int =
|
|||
result += 5
|
||||
|
||||
echo unexpected() # Prints 0, not 10
|
||||
```
|
||||
```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue