5 ms·
> func is now an alias for proc {.noSideEffect.} So excited for this addition! I like to program Nim in a functional lite style, and this is some nice sugar to
by nepeckman 8y ago
> func is now an alias for proc {.noSideEffect.}
So excited for this addition! I like to program Nim in a functional lite style, and this is some nice sugar to write functional procedures. Its also nice that the language reserved the function keyword for procedures that are _actually_ functions.
- treeform 8y agoI want to use the func keword more. I think many of my procs can be funcs instead. In my programs i like having a simple imperative middle, but fuctional no side effects edges.
- narimiran 8y ago> So excited for this addition! I've been using it for a while in the devel version (Nim v0.18.1), and it is great — I like it very much and I completely understand your excitement! Before I didn't bother with {.noSideEffect.} pragma, and now with `func` here — my code is much more clear (at least to me ;)), and I can immediately tell which of my procs are 'pure'.
- k__ 8y agoWhat does it do?
- tyingq 8y agoIt does some static enforcement of "no side effects[1]". [1]https://en.m.wikipedia.org/wiki/Side_effect_(computer_science) https://en.m.wikipedia.org/wiki/Side_effect_(computer_scienc...
- Buttons840 8y agoHow? Even Haskell, who makes a big deal out of separating pure vs impure code, can't do it perfectly.
- tyingq 8y agoI don't think they claim they do it perfectly. You can find bugs related to that, false negatives and positives.
- Buttons840 8y agohttps://nim-lang.org/docs/manual.html#pragmas-nosideeffect-pragma https://nim-lang.org/docs/manual.html#pragmas-nosideeffect-p... It looks really impressive.
- emmelaich 8y ago"Haskell is the world’s best imperative programming language"
- gameswithgo 8y agocan someone expound on what this does exactly and how it works? Will the compiler try to statically verify that it is actually pure and error if it can't? Does it unlock optimizations or other features?
- nepeckman 8y agoYes and yes. From the manual: > If none of its parameters have the type var T or ref T or ptr T this means no locations are modified. It is a static error to mark a proc/iterator to have no side effect if the compiler cannot verify this. I'm not sure how many compiler optimizations are possible, but I do know that procs marked with no side effect are automatically considered thread safe.
- e12e 8y agoWhat if your func iamsafe calls the proc imodifyglobals? More generally, can a "func" call a proc and remain a func?
- nepeckman 8y agoI'm not really a Nim expert, but I did a quick test on the scenario you laid out with these results: iamsafe could not call imodifyglobals, but could call a proc with no side effects. I'd guess that a func can call out to any proc that the compiler knows is side effect free, with the compiler erring on the side of caution.
- dom96 8y agoYep, AFAIK the compiler will do its best to infer the effects and most of the time it does a pretty good job.
- PMunch 8y agoNope, it can only call other side-effect free things. Note however if you call a procedure from a function the function will try to see if the proc is actually side effect free, just not marked as such. So you can still call a proc, but only if it could've been a func.