I've just watched "Why is heskell so hard to learn and how to deal with it"
youtube video. On minute 33 have a quote that "Functional purity is the most
important thing that sets Heskell apart".
As soon as I see this slide, my first though is "wait... I remember that nim
also have this feature. It's {. noSideEffect .} pragma, or just use "func"
instead of "proc" and problem solved.
Here's what I've try
# experiment no side effect
# could nim enforce pure function like Heskell?
import md5
# This is ok, no side effect
func nse1(s:int): string = $s
# This is not ok, echo have side effect
func nse2(s:string): string =
echo s
return s
# This is not ok, read from file have side effect
func nse3(s:string): string = readFile("secret") & s
# This is ok, use var but not modify var
func nse4(s: var string): string = return s & " no side effect"
# This is not ok, use var and modify var
func nse5(s: var string): string =
s = s & "hi"
return s & " no side effect"
# This should ok, why not?
func nse6(s:string): string = $s.toMD5
when(isMainModule):
echo 123.nse1
var s = "hello"
echo s.nse2
echo s.nse3
echo s.nse4
echo s.nse5
echo s #s was modified
echo s.nse6
Run
my question is why nse5 not fail to compile? it's modify input --> s before
call = "hello" while s after call = "hellohi" why nse6 fail to compile? it's
just call md5.toMD5 and not modify anything