Let's say I have this module mymod.nim that's using the tables module:
# mymod.nim
import tables
type
Thing* = object
name*: string
stuff*: Table[int, string]
proc newThing* (): Thing =
result.stuff = initTable[int, string]()
Now I try to use this Thing type in another file like this:
import mymod
var t: Thing = newThing()
t.name = "cool"
t.stuff[0] = "foobazz"
t.stuff[1] = "foobarrsy"
echo t
When I do this I get the following error:
test.nim(6, 8) Error: type mismatch: got (Table[system.int, system.string], int literal(0), string)
but expected one of:
proc `[]=`[I: Ordinal, T, S](a: T; i: I; x: S)
proc `[]=`[Idx, T](a: var array[Idx, T]; x: Slice[Idx]; b: openArray[T])
proc `[]=`(s: var string; x: Slice[int]; b: string)
proc `[]=`[T](s: var seq[T]; x: Slice[int]; b: openArray[T])
proc `[]=`[Idx, T](a: var array[Idx, T]; x: Slice[int]; b: openArray[T])
Now, if I go ahead and 'import tables' along with 'import mymod' then it works just fine:
import mymod
import tables # add this
var t: Thing = newThing()
t.name = "cool"
t.stuff[0] = "foobazz"
t.stuff[1] = "foobarrsy"
echo t # compiles and runs just fine now
I was surprised by this. I guess I naively thought that since mymod imports tables, that any users of mymod would automatically have what they need in order for things to work. I think I'm fundamentally misunderstanding the module system. Any hints are greatly appreciated.
from your module mymod.nim you can do
import tables
export tables # which will export all `tables` symbols from your module
...
see manual: export-statement