ccrma / chuck

ChucK Music Programming Language
http://chuck.stanford.edu/
GNU General Public License v2.0
803 stars 128 forks source link

Added += (add.replace) otf command #363

Closed nshaheed closed 12 months ago

nshaheed commented 1 year ago

This command combines the chuck + and chuck = otf commands:

chuck += 1 a.ck will replace shred 1 with a.ck. If shred 1 doesn't exist, it will create a new one.

Example:

The client:

> ./chuck += 42 a.ck
> ./chuck += 42 a.ck

The server:

> ./chuck.exe --loop
[chuck]: (VM) sporking incoming shred: 42 (a.ck)...
0 :(int)
1 :(int)
2 :(int)
3 :(int)
4 :(int)
[chuck]: (VM) replacing shred 42 (a.ck) with 42 (a.ck)...
0 :(int)
1 :(int)
2 :(int)
3 :(int)
4 :(int)

I'm not sure there's really a way to add a unit test for this. Let me know if there is! Also the --help output is a little weird with the multi-character otf commands. Let me know if you think of a better way to do that

gewang commented 1 year ago

interesting! indeed, is there a single char we can use for +=? also, this feature feels less like "add replace" (which might be a bit confusing, since "replace" already involves an "add" just conditioned on the presence of the shred ID to be replaced) — and more like a smarter "replace"?

Variation 1 (on the add-replace theme): what if we expanded =:

Variation 2, also based around # but with +:

For both variations, the AND_AND_REPLACE message handling could be the same as is in PR (or could be integrated into REPLACE with an additional argument); would just need the initial parsing for # and something like a new int Machine.replace( int id, string path, int alwaysAdd )

Thoughts?

nshaheed commented 1 year ago

interesting! indeed, is there a single char we can use for +=? also, this feature feels less like "add replace" (which might be a bit confusing, since "replace" already involves an "add" just conditioned on the presence of the shred ID to be replaced) — and more like a smarter "replace"?

I can see that, this feature mostly came about because I was using command line arguments to fiddle with numbers around. So I was starting and stopping the code a lot and I was getting annoyed that I had to keep track of whether I needed to use + or = when they were functionally the same for my situation. So I was already primed on using the add and replace verbs.

I guess maybe ~ would be related (as a sort of quasi-replace)? Although tidle being the home directory might add weird edge cases?

Variation 1 (on the add-replace theme): what if we expanded =:

chuck = 42 a.ck still works as before
chuck = #42 a.ck 42 removed if it exists; a.ck is added regardless

This could be powerful! That way you could mix both replace and this new behavior with one command, like this:

chuck = #42 a.ck 43 b.ck # add or replace on 42 and only replace 43

Variation 2, also based around # but with +:

chuck + a.ck business as usual
chuck + a.ck #42 add a.ck, and take out shred 42 (if it currently exists)

At least in my specific use case where I think this is relevant (namely speeding up variable twiddling via command line arguments) I don't like this approach as much because when you grab the previous command from the command line, most terminals put your cursor at the end. So if I'm wanting to change arguments, the shred number being at the end means there's extra keystrokes. But, I have no idea if people would use it that way.

For both variations, the AND_AND_REPLACE message handling could be the same as is in PR (or could be integrated into REPLACE with an additional argument); would just need the initial parsing for # and something like a new Yeah that might make for less repetition in the code. int Machine.replace( int id, string path, int alwaysAdd )

It would definitely be good to add this to the Machine class! Though I think there's a good argument to be made that if you're providing a boolean argument in a situation like this you might as well have a separate function anyway.