I'm trying to figure out the compiler rules for how items are discovered. I've got some code that works when it's in a single file, but fails when spread across two files.
Here is the working code: https://play.nim-lang.org/#ix=3Pwu
But when I take that same code and spread it across two files, it fails:
# entry.nim
import groups
var builder = newGroupBuilder[string]()
echo $builder
# groups.nim
import sets
type
GroupBuilder*[T] = object
groups: HashSet[HashSet[T]]
proc newGroupBuilder*[T](): GroupBuilder[T] =
result.groups = initHashSet[HashSet[T]]()
proc `$`*[T](builder: GroupBuilder[T]): string =
result.add("{")
for group in builder.groups:
result.add("[")
for item in group:
result.add($item)
result.add(",")
result.add("]")
result.add("}")
With the following error message:
/tmp/example/entry.nim(4, 6) template/generic instantiation of `$` from here
/tmp/example/groups.nim(12, 25) Error: type mismatch: got <HashSet[HashSet[system.string]]>
but expected one of:
iterator items(a: cstring): char
first type mismatch at position: 1
required type for a: cstring
but expression 'builder.groups' is of type: HashSet[HashSet[system.string]]
iterator items(a: string): char
first type mismatch at position: 1
required type for a: string
but expression 'builder.groups' is of type: HashSet[HashSet[system.string]]
iterator items[IX, T](a: array[IX, T]): T
first type mismatch at position: 1
required type for a: array[IX, T]
but expression 'builder.groups' is of type: HashSet[HashSet[system.string]]
iterator items[T: Ordinal](s: Slice[T]): T
first type mismatch at position: 1
required type for s: Slice[items.T]
but expression 'builder.groups' is of type: HashSet[HashSet[system.string]]
iterator items[T: char](a: openArray[T]): T
first type mismatch at position: 1
required type for a: openArray[T: char]
but expression 'builder.groups' is of type: HashSet[HashSet[system.string]]
iterator items[T: enum and Ordinal](E: typedesc[T]): T
first type mismatch at position: 1
required type for E: typedesc[T: enum and Ordinal]
but expression 'builder.groups' is of type: HashSet[HashSet[system.string]]
iterator items[T: not char](a: openArray[T]): lent2 T
first type mismatch at position: 1
required type for a: openArray[T: not char]
but expression 'builder.groups' is of type: HashSet[HashSet[system.string]]
iterator items[T](a: seq[T]): lent2 T
first type mismatch at position: 1
required type for a: seq[T]
but expression 'builder.groups' is of type: HashSet[HashSet[system.string]]
iterator items[T](a: set[T]): T
first type mismatch at position: 1
required type for a: set[T]
but expression 'builder.groups' is of type: HashSet[HashSet[system.string]]
expression: items(builder.groups)
I found that manually adding the items call into my loop fixes this specific example, but it's still not a workable solution because it extends to methods in the core library. For example the HashSet difference method depends on the items proc.
> nim --version
Nim Compiler Version 1.6.4 [Linux: amd64]
Compiled at 2022-02-09
Copyright (c) 2006-2021 by Andreas Rumpf
git hash: 7994556f3804c217035c44b453a3feec64405758
active boot switches: -d:release
https://github.com/nim-lang/Nim/issues/11167 https://github.com/nim-lang/Nim/issues/8677
export sets.items in groups.nim workarounds that