Sorry if this is a dumb question; I just started my first real Nim project a few days ago. Why does this proc yield Error: 'crossover' can have side effects?
import random
func crossover*[T](parent_1: seq[T], parent_2: seq[T]): (seq[T], seq[T]) =
## The default crossover function.
## Takes two parents and chooses a random index at which to recombine them.
let idx = rand(0..<parent_1.high)
return (parent_1[0..idx] & parent_2[idx+1..parent_2.high], parent_2[0..idx] &
parent_1[idx+1..parent_1.high])
echo crossover(@[1, 2, 3], @[4, 5, 6])
From looking at it, it doesn't modify anything, so I can't figure out why it is giving the side effect error? Does instantiating the idx variable cause the error?
Why does this proc yield Error: 'crossover' can have side effects?
You are using rand which makes this proc non-deterministic, i.e. you will not always get the same result for the same input.
To expand a bit further the rand procedure updates the global random state object. If you still want a random in this case you can use the initRand to create your own random state object and then pass that to rand like so: https://play.nim-lang.org/#ix=27DN.
Furthermore, like miran pointed out func ensures that your procedure can't have side-effects. If you want what is (erroneously) called a function in most other languages you can use proc.