I try to apply the os.walkDir() method within a closure and got the feeling 
that I might have misunderstood something. In particular, running
    
    
    1  import os
    2
    3  proc walk(obj: string): iterator(): string =
    4    result = iterator(): string {.closure.} =
    5      for k,p in walkDir(obj):
    6        yield p
    7
    8  var g = walk(".")
    9  while not finished(g):
    10   echo "---> ", g()

crashes with the error message
    
    
    ...
    Hint: operation successful (21553 lines compiled; 1.025 sec total; 
34.004MiB; Debug Build) [SuccessX]
    Hint: /home/jordan/Desktop/Devel/deadpan/src/crash . [Exec]
    ---> ./nim.sh
    Traceback (most recent call last)
    crash.nim(10)            crash
    crash.nim(6)             :anonymous
    SIGSEGV: Illegal storage access. (Attempt to read from nil?)
    Error: execution of an external program failed: 
'/home/jordan/Desktop/Devel/deadpan/src/crash '

I went through a session with the GDB but have not much experience with NIM, 
yet. It seems that it crashes in the GC. I tried to compile with different GCs 
(including none) without luck so it might crash for another reason? 
Interestingly, replacing the while loop 8..10 above with
    
    
    8  for w in walk(".")():
    9    echo "---> ", w
    10

prints an infinite series of the same file (repeatedly restarting 
walk(".")()?), in my case
    
    
    ---> ./nim.sh
    ---> ./nim.sh
    ...

while
    
    
    8  var g = walk(".")
    9  for w in g():
    10   echo "---> ", w

crashes similar to the original while loop.

Has somebody come across successfully encapsulating walkDir() into a closure?

Reply via email to