I'd like to implement a simple {.persistence.} template macro ... but I can't 
even get the following hardcoded version to run without resorting to removing 
_history_ as a parameter (i.e. just including it as a global). While that 
works... it isn't particulary _re-use_ friendly :-(.

Any better suggestions for working around generated error:
    
    
    Error: 'history' is of type <var History> which cannot be captured as
    it would violate memory safety, declared here:
    /Users/DM/work/trunk/nim/test_persistence.nim(10, 11); using
    '-d:nimNoLentIterators' helps in some cases
    
    
    Run
    
    
    # Example of proc memory capture violation
    
    import
      flatty, # https://github.com/treeform/flatty - for actual persistence
      std/[exitprocs, os, times]
    
    type
      HistoryEntry  = string
      History = seq[HistoryEntry]
    
    proc save(history: var History, filename: string) =
      writeFile(filename, history.toFlatty)
    
    proc persistence(history: var History, filename: string) =
      if fileExists filename:
        history = filename.readFile.fromFlatty History
      
      addExitProc(
        proc() =
          save(history, filename)
          # Error: 'history' is of type <var History> which cannot be captured 
as
          # it would violate memory safety, declared here:
          # /Users/DM/work/trunk/nim/test_persistence.nim(10, 11); using
          # '-d:nimNoLentIterators' helps in some cases
      )
    
    # Example usage
    const historyFilename = "history.his"
    var history: History = @[]
    
    history.persistence historyFilename
    echo history # so far
    history.add "Another entry: " & $now() # Add another entry on each run
    
    
    Run

Reply via email to