Hi,
I just started to learn nim and play around with it. I am trying to an intersection on two sets but it is not working.
from std/sets import toHashSet, `*`
let b = toHashSet("abc")
let c = toHashSet("cde")
echo (b * c)
Log:
template/generic instantiation of `intersection` from here
Error: type mismatch: got <HashSet[system.char]>
but expected one of:
iterator items(a: cstring): char
first type mismatch at position: 1
required type for a: cstring
but expression 's1' is of type: HashSet[system.char]
iterator items(a: string): char
first type mismatch at position: 1
required type for a: string
but expression 's1' is of type: HashSet[system.char]
iterator items[IX, T](a: array[IX, T]): T
first type mismatch at position: 1
required type for a: array[IX, T]
but expression 's1' is of type: HashSet[system.char]
iterator items[T: Ordinal](s: Slice[T]): T
first type mismatch at position: 1
required type for s: Slice[items.T]
but expression 's1' is of type: HashSet[system.char]
iterator items[T: char](a: openArray[T]): T
first type mismatch at position: 1
required type for a: openArray[T: char]
but expression 's1' is of type: HashSet[system.char]
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 's1' is of type: HashSet[system.char]
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 's1' is of type: HashSet[system.char]
iterator items[T](a: seq[T]): lent2 T
first type mismatch at position: 1
required type for a: seq[T]
but expression 's1' is of type: HashSet[system.char]
iterator items[T](a: set[T]): T
first type mismatch at position: 1
required type for a: set[T]
but expression 's1' is of type: HashSet[system.char]
expression: items(s1)
If I do import std/sets it works. Somebody see what I am doing wrong?
I tried it myself and there is another problem here. The intersection proc (which * is sugar for) is generic and dynamically tries to use the symbol items, which isn't imported here so can't get resolved. I would guess this is specific to the implicit items iterator, and I vaguely remember this being fixed in the development version. But I might be wrong.
Importing both items and $ from std/sets fixes it. If you don't import $ you get the default implementation of $ for objects in the system module which gives less readable output.
@xigoi and @PMunch. I just started learning the language and maybe because of my background (not python) I like to be explicit what I import if I can to avoid conflicts. I read the tutorials on the website and in this part https://nim-lang.org/docs/tut1.html#modules the option of the from is explained with no disclaimer, but maybe I didn't read it fully/correctly.
From now I will use the import statements without the from till I grasp the language and conventions better.
Thanks