splitted echo into echo/debugEcho
This commit is contained in:
parent
23c3af80f6
commit
a783077cfd
3 changed files with 8 additions and 19 deletions
|
|
@ -19,4 +19,5 @@
|
||||||
# * checks for invalid usages of PNimNode
|
# * checks for invalid usages of PNimNode
|
||||||
# * later: will do an escape analysis for closures at least
|
# * later: will do an escape analysis for closures at least
|
||||||
|
|
||||||
|
proc sem2(c: PContext, n: PNode): PNode =
|
||||||
|
nil
|
||||||
|
|
|
||||||
|
|
@ -44,19 +44,6 @@ Fortunately Nimrod supports side effect analysis:
|
||||||
echo "side effect!"
|
echo "side effect!"
|
||||||
result = 55
|
result = 55
|
||||||
|
|
||||||
echo f() * 2 # and still optimized ;-)
|
|
||||||
|
|
||||||
The reason is that the compiler does not consider 'echo' to have side effects
|
|
||||||
for debugging convenience (this is comparable to Haskell's ``UnsafeIO`` monad).
|
|
||||||
So lets try it with a "real" side effect:
|
|
||||||
|
|
||||||
.. code-block:: nimrod
|
|
||||||
template optMul{`*`(a, 2)}(a: int{noSideEffect}): int = a+a
|
|
||||||
|
|
||||||
proc f(): int =
|
|
||||||
writeln stdout, "side effect!"
|
|
||||||
result = 55
|
|
||||||
|
|
||||||
echo f() * 2 # not optimized ;-)
|
echo f() * 2 # not optimized ;-)
|
||||||
|
|
||||||
So what about ``2 * a``? We should tell the compiler ``*`` is commutative. We
|
So what about ``2 * a``? We should tell the compiler ``*`` is commutative. We
|
||||||
|
|
|
||||||
|
|
@ -1648,7 +1648,7 @@ else:
|
||||||
|
|
||||||
proc add*(x: var cstring, y: cstring) {.magic: "AppendStrStr".}
|
proc add*(x: var cstring, y: cstring) {.magic: "AppendStrStr".}
|
||||||
|
|
||||||
proc echo*[T](x: varargs[T, `$`]) {.magic: "Echo", noSideEffect.}
|
proc echo*[T](x: varargs[T, `$`]) {.magic: "Echo".}
|
||||||
## special built-in that takes a variable number of arguments. Each argument
|
## special built-in that takes a variable number of arguments. Each argument
|
||||||
## is converted to a string via ``$``, so it works for user-defined
|
## is converted to a string via ``$``, so it works for user-defined
|
||||||
## types that have an overloaded ``$`` operator.
|
## types that have an overloaded ``$`` operator.
|
||||||
|
|
@ -1656,10 +1656,11 @@ proc echo*[T](x: varargs[T, `$`]) {.magic: "Echo", noSideEffect.}
|
||||||
## available for the ECMAScript target too.
|
## available for the ECMAScript target too.
|
||||||
## Unlike other IO operations this is guaranteed to be thread-safe as
|
## Unlike other IO operations this is guaranteed to be thread-safe as
|
||||||
## ``echo`` is very often used for debugging convenience.
|
## ``echo`` is very often used for debugging convenience.
|
||||||
##
|
|
||||||
## As a special semantic rule, ``echo`` pretends to be free of
|
proc debugEcho*[T](x: varargs[T, `$`]) {.magic: "Echo", noSideEffect.}
|
||||||
## side effects, so that it can be used for debugging routines marked as
|
## Same as ``echo``, but as a special semantic rule, ``debugEcho`` pretends
|
||||||
## ``noSideEffect``.
|
## to be free of side effects, so that it can be used for debugging routines
|
||||||
|
## marked as ``noSideEffect``.
|
||||||
|
|
||||||
template newException*(exceptn: typeDesc, message: string): expr =
|
template newException*(exceptn: typeDesc, message: string): expr =
|
||||||
## creates an exception object of type ``exceptn`` and sets its ``msg`` field
|
## creates an exception object of type ``exceptn`` and sets its ``msg`` field
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue