effects.txt 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. =====================================================================
  2. Side effects in Nim
  3. =====================================================================
  4. Note: Side effects are implicit produced values! Maybe they should be
  5. explicit like in Haskell?
  6. The idea is that side effects and partial evaluation belong together:
  7. Iff a proc is side effect free and all its argument are evaluable at
  8. compile time, it can be evaluated by the compiler. However, really
  9. difficult is the ``newString`` proc: If it is simply wrapped, it
  10. should not be evaluated at compile time! On other occasions it can
  11. and should be evaluated:
  12. ```nim
  13. proc toUpper(s: string): string =
  14. result = newString(len(s))
  15. for i in 0..len(s) - 1:
  16. result[i] = toUpper(s[i])
  17. ```
  18. No, it really can always be evaluated. The code generator should transform
  19. ``s = "\0\0\0..."`` back into ``s = newString(...)``.
  20. ``new`` cannot be evaluated at compile time either.
  21. Raise statement
  22. ===============
  23. It is impractical to consider ``raise`` as a statement with side effects.
  24. Solution
  25. ========
  26. Being side effect free does not suffice for compile time evaluation. However,
  27. the evaluator can attempt to evaluate at compile time.