effects.txt 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 evaluted:
  12. .. code-block:: 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. No, it really can always be evaluated. The code generator should transform
  18. ``s = "\0\0\0..."`` back into ``s = newString(...)``.
  19. ``new`` cannot be evaluated at compile time either.
  20. Raise statement
  21. ===============
  22. It is impractical to consider ``raise`` as a statement with side effects.
  23. Solution
  24. ========
  25. Being side effect free does not suffice for compile time evaluation. However,
  26. the evaluator can attempt to evaluate at compile time.