Re: Sequence is unchangeable when passed to a spawned proc

2016-12-23 Thread pegra
Thank you for your posts.

I think the behavior is a matter of the Nim compiler. aSeq is deepcopied into 
aSeqPar and aSeqPar behaves as if it is declared locally within workerTread as 
"let aSeqPar = aSeq". But I would prefer: "var aSeqPar = aSeq". Obviously there 
exists no suitable pragma too.


Sequence is unchangeable when passed to a spawned proc

2016-12-21 Thread pegra
Please look at **Why not?** at the right.

But it works when the sequence is copied.

Could't this unnecessary overhead be avoided?


import sequtils, os, threadpool

var aSeq = @[10, 20, 30]
echo "aSeq=", repr(aSeq)

proc workerThread(aSeqPar: seq[int]) =
  # aSeqPar is a copy of aSeq now
  var localSeq = aSeqPar  # localSeq is a copy of aSeqPar
  echo "localSeq=", repr(localSeq)
  localSeq[1] = 40  # works
  echo "localSeq=", localSeq
  echo "aSeqPar=", repr(aSeqPar)
#  aSeqPar[2] = 50  # Error: 'aSeqPar[2]' cannot be assigned toWhy 
not?
  echo "aSeqPar=", aSeqPar

spawn workerThread(aSeq)
sync()
echo "aSeq=", repr(aSeq)

#[ output:
aSeq=00196360[10, 20, 30]
localSeq=01F2F088[10, 20, 30]
localSeq=@[10, 40, 30]
aSeqPar=01F2F050[10, 20, 30]
aSeqPar=@[10, 20, 30]
aSeq=00195360[10, 20, 30]
]#