nim-wiki/Common-Criticisms.md

12 lines
815 B
Markdown
Raw Normal View History

2014-10-21 23:11:12 +00:00
## Callsite annotations on var parameters would decrease mistakes
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.