Just had an idea, not sure if it makes sense.
When I was writing a code that executes a Markov algorithm, I had to call std.array.replace(R, R, R), then depending on whether the replacement had made any actual changes, I had to either continue the loop or redo it. In order to find out if replace had made any changes I had to keep a copy and compare on every iteration.
The relevant part of the code:
redo:
auto copy = tests[i];
foreach (c; capt) {
tests[i] = replace(tests[i], c[0], c[2]);
if (c[1] == ".") break;
if (tests[i] != copy) goto redo;
}
It occurred to me that it would have been useful if the replace function had had an out parameter keeping track of the number of changes. My code could have looked like this:
redo:
int changed;
foreach (c; capt) {
tests[i] = replace(tests[i], c[0], c[2], changed);
if (c[1] == ".") break;
if (changed) goto redo;
}
I realize that you can't meet everybody's needs and preferences, but I thought I'd suggest it just in case it might be doable.
Comment #1 by andrei — 2016-10-14T15:38:19Z
This seems to have merit. Perhaps an optional last ref size_t parameter would fit the bill.
Comment #2 by razvan.nitu1305 — 2017-07-07T10:00:58Z
(In reply to Andrei Alexandrescu from comment #1)
> This seems to have merit. Perhaps an optional last ref size_t parameter
> would fit the bill.
You can't have optional ref parameters. At the moment the language only supports default parameters (which we consider optional), but having a ref size_t parameter would imply something along the lines of foo(ref size_t a = value), where value would be the address of the parameter. We can still implement this by overloading the function
Comment #3 by andrei — 2017-07-07T13:44:18Z
"Optional" here means "an overload with 4 parameters"
Comment #4 by dlang-bot — 2023-02-28T21:21:09Z
@gizmomogwai updated dlang/phobos pull request #8694 "Try to implement https://issues.dlang.org/show_bug.cgi?id=6106" fixing this issue:
- fix Issue 6106 - Keep track of changes during replace function
The functions without the newly introduced parameter forward
to the full implementation to reduce code duplication.
https://github.com/dlang/phobos/pull/8694
Comment #5 by dlang-bot — 2023-03-05T11:49:34Z
dlang/phobos pull request #8694 "fix Issue 6106 - Keep track of changes during replace function" was merged into master:
- 69be5ca021246cc01b20be98d10f9886dd0625fb by Christian Koestlin:
fix Issue 6106 - Keep track of changes during replace function
The functions without the newly introduced parameter forward
to the full implementation to reduce code duplication.
https://github.com/dlang/phobos/pull/8694