D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6106 - Keep track of changes during replace function
Summary: Keep track of changes during replace function
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P5 enhancement
Assignee: dcuan17
URL:
Keywords: bootcamp, pull
Depends on:
Blocks:
 
Reported: 2011-06-04 19:15 UTC by josvanuden@gmail.com
Modified: 2023-03-05 11:49 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description josvanuden@gmail.com 2011-06-04 19:15:09 UTC
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 Andrei Alexandrescu 2016-10-14 15:38:19 UTC
This seems to have merit. Perhaps an optional last ref size_t parameter would fit the bill.
Comment 2 RazvanN 2017-07-07 10:00:58 UTC
(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 Andrei Alexandrescu 2017-07-07 13:44:18 UTC
"Optional" here means "an overload with 4 parameters"
Comment 4 Dlang Bot 2023-02-28 21:21:09 UTC
@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 Dlang Bot 2023-03-05 11:49:34 UTC
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