Hi,

I would like to use Nim for javascript related work, and I wonder how I can use 
event handlers in Nim when the proc I want to register is a closure. Often, the 
event needs to access external data and gets the `{.closure.}` annotation, and 
I get the following error from the compiler:
    
    
    Error: type mismatch: got <proc (event: Event){.closure, noSideEffect, 
gcsafe, locks: <unknown>.}> but expected 'proc (event: Event)'
    
    
    Run

In JavaScript, it's easy to convert a closure to a function as functions are 
already first-class objects, but how to tell the compiler? Else, can I work 
around this?

I also have a related side questions, it seems that the javascript backend does 
not supports iterators (closure iterators), and I was just wondering if there 
was a technical reason to that or if it was just that this was not yet 
implemented in the compiler. I end up having to create iterators objects 
manually which makes some boilerplate that is strange for a language where 
first-class iterators are supported:
    
    
    type
      ProcIter*[D2]             = proc(): tuple[ok: bool, data: D2]
      ProcIterator*[D1,D2]      = proc(d: D1): ProcIter[D2]
    
    proc seqIterator*[D](arr: seq[D]): ProcIter[D] =
      #mixin items
      #var it = items # Instanciate iterator
      var it = 0
      var empty: D
      
      proc next(): tuple[ok: bool, data: D] =
        #let item: D = it(data)
        #if finished(it): return (false, item)
        #else: return (true, data)
        if it >= len(arr): return (false, empty)
        result = (true, arr[it])
        it = it + 1
      
      return next
    
    
    
    Run

And instead of a nice for loop, I have to write:
    
    
    var itf = match.iterate(val)
    while true:
      var it = itf()
      if it[0] == false: break
      var item = it[1]
      # ...
    
    
    Run

Reply via email to