I'm trying to do something relatively simple but it's not working. I'm defining a distinct string and then attempting to borrow slice assignment and it's not working, saying that there is no symbol to borrow from:
type Dna* = distinct string
proc `[]=`*[T, U](s: var Dna, x: HSlice[T, U], b: Dna) {.borrow.}
Here's the error:
test.nim(7, 14) Error: no symbol to borrow from found
Why would that be? []= is defined for strings in system, so it should just work.
As a follow up for potential future readers, this does work:
proc `$`*(x: Dna): string {.borrow.}
proc `[]=`*[T, U](s: var Dna, x: HSlice[T, U], b: Dna) =
var tmp = $s
s[x] = b
s = tmp.Dna
This seems inefficient but does work around the problem.
proc `[]=`*[T, U](s: var Dna, x: HSlice[T, U], b: Dna) = s.string[x] = b.string
should work too
Thanks for the pointer! I think I was missing something basic, so let me verify that I understand (after playing around with the semantics):
$s returns a copy of s, even though s is a distinct string. In contrast, s.string is a "cast" that lets you do string operations directly on a distinct string. For example, I can't do something like $x[0..1]= "AT" but can do x.string[0..1] = "AT". Is that right?
s.string is a "cast"
in nim cast is unsafe byte reinterpretation, this is just type conversion.
For example, I can't do something like $x[0..1]= "AT" but can do x.string[0..1] = "AT". Is that right?
yep