#converter toString(a: seq[byte]): string {.inline.} = cast[ptr string](a.addr)[] var query = @[byte 1,2,3] query[0..1] = @[byte 8,8] echo query # outputs: @[8, 8, 3]
with converter:
test.nim(4, 6) template/generic instantiation of `[]=` from here D:\Programm\Nim\Nim-devel\lib\system\indices.nim(158, 15) template/generic instantiation of `spliceImpl` from here D:\Programm\Nim\Nim-devel\lib\system\indices.nim(68, 56) Error: 's[i`gensym0]' cannot be assigned to
in this small example i don't even use strings, so why it interferes with assignment to a slice of sequence?
nim -v Nim Compiler Version 2.1.1 [Windows: amd64] Compiled at 2023-11-19 Copyright (c) 2006-2023 by Andreas Rumpf git hash: cecaf9c56b1240a44a4de837e03694a0c55ec379 active boot switches: -d:release
Looks like a bug to me, please report it on github. You can also add code example below.
in this small example i don't even use strings
Converters enable implicit conversion. And combined with templates (used in the system library) it causes unexpected results:
template inspect(a: typed) =
if a is seq and a.len > 0:
if a[0] is byte:
echo "It is a sequence of bytes!"
elif a[0] is char:
echo "It is a sequence of chars!"
else:
echo "It is something else."
converter `toString`(a: seq[byte]): string = cast[ptr string](a.addr)[]
echo char is byte # false
inspect(@[byte 1, 2, 3]) # compiles and outputs: it is a sequence of chars!
Consider using explicit conversion instead:
proc unsafeString(a: seq[byte]): string {.inline.} = cast[ptr string](a.addr)[]
echo @[byte 1, 2, 3].unsafeString() # outputs: ^A^B^C
var query = @[byte 1,2,3]
query[0..1] = @[byte 8,8]
echo query # outputs: @[8, 8, 3]