cowstrings icon indicating copy to clipboard operation
cowstrings copied to clipboard

Fix the []= proc

Open planetis-m opened this issue 3 years ago • 5 comments

planetis-m avatar Jul 29 '21 20:07 planetis-m

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.

planetis-m avatar Nov 26 '21 16:11 planetis-m

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

Araq avatar Dec 06 '22 00:12 Araq

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.

planetis-m avatar Dec 06 '22 04:12 planetis-m

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

Araq avatar Dec 06 '22 05:12 Araq

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

Araq avatar Dec 06 '22 05:12 Araq