This commit is contained in:
flaviut 2014-05-29 16:23:04 -04:00
commit f1a45ff3de
2 changed files with 25 additions and 20 deletions

View file

@ -9,32 +9,37 @@ proc getAlphabet(): string =
result.add(letter) result.add(letter)
const abcs = getAlphabet() const abcs = getAlphabet()
var var
foo = "bar" a = "foo"
bar = "foo" b = 0
# oops, I need to fix the names
let temp = foo
foo = bar
bar = temp
echo "abcs is ", abcs let
echo "foo is ", foo c = "foo"
echo "bar is ", bar 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 $ nimrod c --verbosity:2 ./assignment.nim
abcs is abcdefghijklmnopqrstuvwxyz a20.nim(19, 0) Error: for a 'var' type a variable needs to be passed
foo is foo c.add "bar" # This will result in an error.
bar is bar ^
``` ```
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 ``` C
STRING_LITERAL(TMP129, "abcdefghijklmnopqrstuvwxyz", 26); 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.

View file

@ -1,15 +1,15 @@
# Result # 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 ``` nimrod
proc expr(number, power: int): int = proc `**`(number, power: int): int =
result = number result = number
for i in 1..power: for i in 1..power:
result *= number 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 ``` nimrod
proc unexpected(): int = proc unexpected(): int =