microsoft / qsharp-compiler

Q# compiler, command line tool, and Q# language server
https://docs.microsoft.com/quantum
MIT License
682 stars 171 forks source link

Code action suggesting replacement for array item updates #78

Closed sw23 closed 5 years ago

sw23 commented 5 years ago

Arrays in Q# are immutable, meaning array items cannot be updated once defined. See Local Variables for more details.

As a result, the following code results in a compilation error:

set arr[i] = 1;

Instead, a Copy-and-Update Expression should be used to create a new array with the modified value:

set arr w/= i <- 1;

Since array item updates are a common thing for users to want to do, it'd be nice to have a code action suggesting the copy-and-update expression corresponding to the array item update specified. This can obviously get fairly complicated, but handling the common/easy cases would go a long way.

vadym-kl commented 5 years ago

This looks like a great code action, maybe you could also support it for arr[i] = 1 in addition to set arr[i] = 1. It could be quite common ( this is my subjective opinion) for people who are just learning Q# to just want to write arr[i] = 1 for array assignment if they are already familiar with C#, Java, C or other similar languages.

If there were a code action expanding arr[i][j] = 1 into set arr w/= i <- ( set arr[j] w/= j <- 1 ); that also could be very helpful.

bettinaheim commented 5 years ago

The corresponding PR is https://github.com/microsoft/qsharp-compiler/pull/91.

bettinaheim commented 5 years ago

The code action as filed above is merged into master with PR #130. I'll close this issue for now. @sw23 , @vadym-kl let me know if you are interested in picking up implementing additional support for the cases that Vadym mentioned.

sw23 commented 5 years ago

The code action as filed above is merged into master with PR #130. I'll close this issue for now. @sw23 , @vadym-kl let me know if you are interested in picking up implementing additional support for the cases that Vadym mentioned.

@bettinaheim - Cool! Yeah I can update the code action to support the additional cases that @vadym-kl mentioned now that the initial one has been reviewed and almost pulled in. Should I open a follow-on issue?

bettinaheim commented 5 years ago

@sw23 Awesome! Yes, opening a new issue would be good - I'd rather close issues as the planned things get done, and open new issues for addtional capabilities. I am looking forward to those! :) Let me know if you need/want help!