I am trying to understand the problem depicted in this [Stack Overflow post — 
implementing map min that takes the tables keys iterator as argument in 
nim](https://stackoverflow.com/questions/70240040/implementing-map-min-that-takes-the-tables-keys-iterator-as-argument-in-nim).

The author of the post tries three attempts to define a map function that would 
work for tables. I guess there is a type error with its first attempt:
    
    
    iterator map[A, B](iter: iterable[A], fn: A -> B): B =
      for x in iter:
        yield fn(x)
    
    
    Run

When I try its code, I have this:
    
    
    import std/[sugar, tables]
    # A mapping from coordinates (x, y) to values.
    var locations = initTable[(int, int), int]()
    
    locations[(1, 2)] = 1
    locations[(2, 1)] = 2
    locations[(-2, 5)] = 3
    
    echo typeof(locations.keys)
    echo locations.keys is (int, int)
    echo locations.keys is iterable[(int, int)]
    
    
    Run

I get:
    
    
    (int, int)
    true
    false
    
    
    Run

while I would expect:
    
    
    iterable[(int, int)]
    false
    true
    
    
    Run

I don't understand why I don't have an iterable type there. Is this due to the 
`lent` return type ? See the prototype to the keys iterator:
    
    
    iterator keys[A, B](t: Table[A, B]): lent A
    
    
    Run

(I did not find any mention about the `lent` return type in the 
[manual](https://nim-lang.org/docs/manual.html), but got a description in 
[destructors 
documentation](https://nim-lang.org/docs/destructors.html#lent-type) which is 
not an obvious place to look for an unknown system type.)

Can we avoid this with another prototype in the standard library ? Like:
    
    
    iterator keys[A, B](t: Table[A, B]): lent iterable[lent A]
    
    
    Run

Reply via email to