Hi, I am actively using Nim's side effect tracking system and marking procs with noSideEffect pretty much everywhere. I have external requirement not to have side effects and Nim helps here quite a bit.

However, I have place in a code that logically I don't have side effects, but physically I am changing global cache map object. I do memoization for perfomance reasons.

Is there a way hide this effect to compiler?

2017-11-10 15:42:46
I don't think it's something you should actually do as the compiler can do certain optimizations when it knows a routine has no side effects. Optimizations which may opt out your side effects, I guess. 2017-11-11 23:11:04

There is a way to hide this, but I don't know if it's a good idea for your use case.

proc sideEffect(a, b: string) = echo a, b

proc lacksSideEffect() {.noSideEffect.} =
  cast[proc (a, b: string) {.nimcall, noSideEffect.}](sideEffect)()

2017-11-13 09:12:21
@Araq That looks nasty! Wouldn't it confuse the compiler? Also --- it prevents inlining, I guess?
2017-11-15 12:53:58
Well it's not "nasty", 'cast' is used to circumvent the type system. Probably needs better documentation in the manual. Pretty sure the C backend can see through the cast and inline it anyway.
2017-11-15 14:40:02
@Araq Oh, I shold bold it I mean Nim optimization and inlining, not C's ones... as weird as it sounds. I mean, any pattern-template or pattern-macro which matches a noSideEffect routine would match here, despite the fact this routine has side effects, actually. I guess whenever it's good or not depends on the intended behavior.
2017-11-15 16:38:38
In regards to cdome's original question, I think tricking the compiler to ignore side effects for a certain function is the wrong way to go about this. If the memoization table was passed as a function parameter, the function would have no side effects, with no tricks involved. 2017-11-15 17:14:15

I am fine with Araq's solution.

Passing cache table as argument is not an option, because it needs to be updated on every request which is a side effect. Returning a copy of table on every call is not an option either, as its size is 64GB. It is cache of matrices

2017-11-15 18:32:34
Keep in mind though that this "my code is logically without side effects" is useless in a multi-threaded environment. It can be as logical as you want, the tables will cause threaded code to crash.
2017-11-16 01:33:32