File: effects.txt

package info (click to toggle)
nim 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,911,644 kB
  • sloc: sh: 24,603; ansic: 1,761; python: 1,492; makefile: 1,013; sql: 298; asm: 141; xml: 13
file content (42 lines) | stat: -rw-r--r-- 1,229 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
=====================================================================
               Side effects in Nim
=====================================================================

Note: Side effects are implicit produced values! Maybe they should be
explicit like in Haskell?


The idea is that side effects and partial evaluation belong together:
Iff a proc is side effect free and all its argument are evaluable at
compile time, it can be evaluated by the compiler. However, really
difficult is the ``newString`` proc: If it is simply wrapped, it
should not be evaluated at compile time! On other occasions it can
and should be evaluated:

  ```nim
  proc toUpper(s: string): string =
    result = newString(len(s))
    for i in 0..len(s) - 1:
      result[i] = toUpper(s[i])
  ```

No, it really can always be evaluated. The code generator should transform
``s = "\0\0\0..."`` back into ``s = newString(...)``.


``new`` cannot be evaluated at compile time either.


Raise statement
===============

It is impractical to consider ``raise`` as a statement with side effects.


Solution
========

Being side effect free does not suffice for compile time evaluation. However,
the evaluator can attempt to evaluate at compile time.