I'm trying to represent a distinct seq[byte] and am running into c compiler errors.
Snippet:
type
Bytes = distinct seq[byte]
proc add(x: var Bytes; b: byte) {.borrow.}
func toBytes(s: string): Bytes =
result = newSeqOfCap[byte](s.len).Bytes
for c in s:
result.add c.byte
when isMainModule:
let a = "abc123"
let b = a.toBytes
Error:
...: In function 'toBytes_zNM9cgBdi8WkUZbKyIQHh5g':
...:355:29: error: lvalue required as unary '&' operand
unsureAsgnRef((void**) (&(&result)), (tySequence_6H5Oh5UUvVCLiakt9aTwtUQ**) incrSeqV3(&((&result))->Sup, (&NTI_6H5Oh5UUvVCLiakt9aTwtUQ_)));
^
...:355:103: error: 'result' is a pointer; did you mean to use '->'?
unsureAsgnRef((void**) (&(&result)), (tySequence_6H5Oh5UUvVCLiakt9aTwtUQ**) incrSeqV3(&((&result))->Sup, (&NTI_6H5Oh5UUvVCLiakt9aTwtUQ_)));
^~
->
...:356:20: error: 'result' is a pointer; did you mean to use '->'?
T4_ = (&result)->Sup.len++;
^~
->
...:357:14: error: 'result' is a pointer; did you mean to use '->'?
(&result)->data[T4_] = ((NU8) (((NU8)(c))));
^~
->
Nim Version:
Nim Compiler Version 0.18.1 [Windows: amd64]
Compiled at 2018-09-09
Copyright (c) 2006-2018 by Andreas Rumpf
git hash: 7107ec05de3a231799dbba66564fb2e22e94217a
active boot switches: -d:release
What part of 'toBytes' leads to this error?
What part of 'toBytes' leads to this error?
result.add. You can see the C source in nimcache directory of your project, better in debug mode. Probably it's a bug with {.borrow.}, you can work around by explicitly providing needed operations:
template add(x: var Bytes; b: byte) = seq[byte](x).add b
So its free to convert between base and distinct types like that?
e.g. seq[byte](result) or r.Bytes
like a cast, but without the word cast?
see Distinct Type and the examples that follow.
Explicit type conversions from a distinct type to its base type and vice versa are allowed.