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?
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)()
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
I've found the above trick useful to have a stack trace for debugging deep in a noSideEffect hierarchy, using the following little helper:
proc noSideEffectWriteStackTrace() {.noSideEffect.} =
cast[proc () {.nimcall, noSideEffect.}](writeStackTrace)()
Maybe there are better ways to do the same, but this worked for me.The contemporary way of doing it would be
proc noSideEffectWriteStackTrace() {.noSideEffect.} =
{.cast(noSideEffect).}:
writeStackTrace()