planetis-m/cowstrings

Fix the []= proc

planetis-m opened this issue · 5 comments

Fix the []= proc

Actually... there is nothing to fix. Due to Nim operator overloading always choosing the returning var [] proc overload, when the variable is mutable, one has to do the compiler's job and insert prepareMutation beforehand.

Araq commented

These need to be fixed:

proc `[]`*(x: var String; i: int): var char {.inline.} =
  checkBounds(i, x.len)
  x.p.data[i]

proc `[]=`*(x: var String; i: int; val: char) {.inline.} =
  checkBounds(i, x.len)
  x.p.data[i] = va

For []= I can add a call to prepareMutation but adding it to [] will make copies for every var variable even if it's not mutated at the end. That will make the two procs inconsistent and it's the reason I avoided it.

Araq commented

Maybe don't provide the proc []*(x: var String; i: int): var char version then.

Araq commented

Or maybe use something like assert isUnique(x) so that people cannot forget to call prepareMutation before mutations.