10 months later, MrZoraman's sample code now compiles without errors with nim
1.0.4. But I have the same strange error about type error on `items`, and like
MrZoraman I don't understand how it can happen.
I have 2 modules, `bb.nim` and `cc.nim` that imports `bb.nim`. When compiling
`cc.nim`, I got the error:
$ nim c cc
Hint: used config file '.choosenim/toolchains/nim-1.0.0/config/nim.cfg'
[Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: cc [Processing]
Hint: hashes [Processing]
Hint: bb [Processing]
Hint: tables [Processing]
Hint: math [Processing]
Hint: bitops [Processing]
Hint: macros [Processing]
Hint: algorithm [Processing]
Hint: sets [Processing]
cc.nim(21, 12) template/generic instantiation of `addConstraints` from here
bb.nim(12, 45) template/generic instantiation of `+` from here
.choosenim/toolchains/nim-1.0.0/lib/pure/collections/sets.nim(487, 17)
template/generic instantiation of `union` from here
.choosenim/toolchains/nim-1.0.0/lib/pure/collections/sets.nim(411, 7)
template/generic instantiation of `incl` from here
.choosenim/toolchains/nim-1.0.0/lib/pure/collections/sets.nim(211, 15)
Error: type mismatch: got <HashSet[cc.Constraint]>
but expected one of:
iterator items[T](a: seq[T]): T
first type mismatch at position: 1
required type for a: seq[T]
but expression 'other' is of type: HashSet[cc.Constraint]
iterator items(a: string): char
first type mismatch at position: 1
required type for a: string
but expression 'other' is of type: HashSet[cc.Constraint]
iterator items[IX, T](a: array[IX, T]): T
first type mismatch at position: 1
required type for a: array[IX, T]
but expression 'other' is of type: HashSet[cc.Constraint]
iterator items[T](a: set[T]): T
first type mismatch at position: 1
required type for a: set[T]
but expression 'other' is of type: HashSet[cc.Constraint]
iterator items(a: cstring): char
first type mismatch at position: 1
required type for a: cstring
but expression 'other' is of type: HashSet[cc.Constraint]
iterator items[T](s: HSlice[T, T]): T
first type mismatch at position: 1
required type for s: HSlice[items.T, items.T]
but expression 'other' is of type: HashSet[cc.Constraint]
iterator items[T](a: openArray[T]): T
first type mismatch at position: 1
required type for a: openArray[T]
but expression 'other' is of type: HashSet[cc.Constraint]
iterator items(E: typedesc[enum]): E:type
first type mismatch at position: 1
required type for E: type enum
but expression 'other' is of type: HashSet[cc.Constraint]
expression: items(other)
Run
If I merge both modules in one with `cat bb.nim cc.nim > dd.nim` and comment
out the `import bb` line, then `dd.nim` compiles without errors.
The problem appears on the line:
constraints[choice] = constraints[choice] + consts.toHashSet()
Run
or
constraints[choice].incl(consts.toHashSet())
Run
What the compiler does not like? I don't have partial imports. Where is the
problem? In my code or in the libraries?
**bb.nim**
import tables
import sets
type
Constraints[T, V] = Table[T, HashSet[V]]
proc initConstraints*[T, V](): Constraints[T, V] =
result = Constraints(initTable[T, initHashSet[V]()]())
proc addConstraints*[T, V](constraints: var Constraints[T, V], choice: T,
consts: seq[V]) =
#if choice in constraints:
constraints[choice] = constraints[choice] + consts.toHashSet()
# constraints[choice].incl(consts.toHashSet())
#else:
# constraints[choice] = consts.toHashSet()
Run
**cc.nim**
import hashes
import bb
type
Choice = object
i: int
Constraint = object
j: int
proc hash(c: Choice): Hash =
result = Hash(c.i)
proc hash(c: Constraint): Hash =
result = Hash(c.j)
var constraints = initConstraints[Choice, Constraint]()
let choice = Choice(i: 1)
let constSeq = @[Constraint(j: 2)]
constraints.addConstraints(choice, constSeq)
Run