Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the []= proc #1

Open
planetis-m opened this issue Jul 29, 2021 · 5 comments
Open

Fix the []= proc #1

planetis-m opened this issue Jul 29, 2021 · 5 comments

Comments

@planetis-m
Copy link
Owner

No description provided.

@planetis-m
Copy link
Owner Author

planetis-m commented Nov 26, 2021

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
Copy link

Araq commented Dec 6, 2022

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

@planetis-m
Copy link
Owner Author

planetis-m commented Dec 6, 2022

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
Copy link

Araq commented Dec 6, 2022

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

@Araq
Copy link

Araq commented Dec 6, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants