I would like to convert a string to a seq[char] so I can utilize a few of the procs in sequtils, but I'm having an issue with the toSeq template.
For example:
proc someproc(a, b: string): seq[tuple[a, b: char]] =
let
s1 = toSeq(a)
s2 = toSeq(b)
result = zip(s1, s2)
Gives a compilation error:
lib/pure/collections/sequtils.nim(329, 20) Error: type mismatch: got (seq[string], char)
but expected one of:
system.add(x: var string, y: char)
system.add(x: var string, y: string)
system.add(x: var seq[T], y: T)
system.add(x: var string, y: cstring)
system.add(x: var seq[T], y: openarray[T])
Looking into that template: https://github.com/Araq/Nim/blob/master/lib/pure/collections/sequtils.nim#L292
I'm seeing that the type of the sequence is determined by the iter parameter, which in this case is string instead of char. Is this a bug in the template, or am I using this template incorrectly?
I'm using a fairly recent 10.3 build of Nim
The problem is that your expression is not an iterator in itself. I can get your example to work by calling items, i.e., converting the string explicitly to an iterator:
proc someproc(a, b: string): seq[tuple[a, b: char]] =
let
s1 = toSeq(a.items)
s2 = toSeq(b.items)
result = zip(s1, s2)
Whether or not this qualifies as a bug I don't know. If you call toSeq with anything that does not allow to do for x in <your_expression> than you will get a meaningful error. In your case this does work, since a string can be converted into an iterator. However the type of the return type is determined in the line above, and you will simply get seq[string]. Nevertheless, it probably won't hurt to file an issue on Github.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With