nim-wiki/Common-Criticisms.md

14 lines
1.0 KiB
Markdown
Raw Normal View History

2014-10-21 23:46:56 +00:00
## Nim doesn't require call-site annotation for `var` parameters
This is referring to systems like C#'s: `void foo(ref int myInput){...}; foo(ref a);`. Note the ref on the `foo` call. If this was Nim, it'd be impossible to tell from the call-site that `foo` has the potential to modify `a`.
2014-10-21 23:11:12 +00:00
Possibly. The problem here is that of perception. In many languages, heap allocation through pointers is the only method of having objects, and passing them to a function gives the freedom to modify them. In Nim, things can be allocated on the stack, and those things need to be treated in the same way as things on the heap.
``` nimrod
proc foo(input: ref T) = ...
2014-10-21 23:14:09 +00:00
let a: ref T = ...
2014-10-21 23:11:12 +00:00
foo(a) # valid, this is Java-style
2014-10-21 23:14:09 +00:00
var b: T = ...
2014-10-21 23:11:12 +00:00
foo(b) # also valid and equivalent
2014-10-21 23:14:09 +00:00
```
2014-10-21 23:18:09 +00:00
Note that the difference between what happens in Java and what Nim does is simply a matter of efficiency: Nim does not require our `T` to be allocated on the heap, and it certainly allows `b` to be declared with `let`, which will force an compile-time error to be thrown.