you were right @jackhftang, i wasn't importing the lib. I got that 
`isPowerOfTwo AssertionError` from `declared(tables.rightSize)` being false 
within user.nim, that is frustrating.

found two solutions:

use a proc: 
    
    
    #lrucache.nim
    import tables, lists
    
    type
      Node[K, T] = object
        key: K
        val: T
      LruCache[K, T] = object
        capacity: int
        list: DoublyLinkedList[Node[K, T]]
        table: Table[K, DoublyLinkedNode[Node[K, T]]]
    proc rightSize(cap: int): int =
      when declared(tables.rightSize) and (NimMajor, NimMinor) < (1, 4):
        tables.rightSize(cap)
      else:
        cap
    proc newLruCache*[K, T](capacity: int): LruCache[K, T] =
      LruCache[K, T](
        capacity: capacity,
        list: initDoublyLinkedList[Node[K, T]](),
        table: initTable[K, DoublyLinkedNode[Node[K, T]]](rightSize(capacity))
      )
    
    
    Run
    
    
    #test.nim
    import lrucache
    let z = newLruCache[int,string](5)
    echo z.repr
    
    
    Run

or, export tables from lrucache.nim: 
    
    
    #lrucache.nim
    import tables,lists
    when declared(tables.rightSize) and (NimMajor,NimMinor) < (1,4):
      export tables.rightSize
    type
      Node[K, T] = object
        key: K
        val: T
      LruCache[K, T] = object
        capacity: int
        list: DoublyLinkedList[Node[K, T]]
        table: Table[K, DoublyLinkedNode[Node[K, T]]]
    
    template rightSize(cap): untyped {.dirty.} =
      when declared(lru.rightSize) and (NimMajor, NimMinor) < (1, 4):
        lru.rightSize(cap)
      else:
        cap
    
    proc newLruCache*[K, T](capacity: int): LruCache[K, T] =
      LruCache[K, T](
        capacity: capacity,
        list: initDoublyLinkedList[Node[K, T]](),
        table: initTable[K, DoublyLinkedNode[Node[K, T]]](rightSize(capacity))
      )
    
    
    Run

Reply via email to