fix issue 18631 - Allow choice to work with const arrays#6302
Conversation
|
Thanks for your pull request and interest in making D better, @bbasile! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla references
|
std/random.d
Outdated
| a copy. | ||
| */ | ||
| auto ref choice(Range, RandomGen = Random)(auto ref Range range, | ||
| auto ref choice(Range, RandomGen = Random)(inout ref Range range, |
There was a problem hiding this comment.
Shouldn't this be inout auto ref?
Because I don't think this will compile anymore choice([1, 2, 3, 4]);
There was a problem hiding this comment.
inout implies also auto in this case. I've added a test for a call by value since your comment.
There was a problem hiding this comment.
Honestly, this seems like a compiler bug. I've never heard of inout ref doing anything similar to auto ref, and that behavior does not match what happens with const or immutable. Also, the way inout is designed, it's always supposed to be used on the return value in addition to one of the parameters. So, even if it's true that inout ref is supposed to act like auto ref with regards to rvalues, it would then be a bug that inout isn't used on the return type.
|
Okay done. |
|
Geez inout is so ugly. |
In most cases, with templated functions, you can just not bother with |
|
(manually merging as Darwin_64_32 is currently broken) |
|
This PR is a breaking change, because it assumes that |
|
Any suggestions as to how to fix it, short of reverting the whole thing? Preferably, we would not have to undo everything done here. |
|
This is trying to fix a corner cases in ranges. Either it will work to just slice the array before passing it in, or a separate overload for dynamic arrays will be required. Regardless, marking a range as |
Edit: actually, all you needed to do is put |
|
Yes, The downside of not doing this, is that you lose the "temporary const" guarantee that it provides. |
|
Since it's pretty accepted that you can slice an object and get a range from it, Another way to fix this may be (not a full implementation, needs some tweaking): auto ref choice(R)(auto ref R r) if (!isRandomAccessRange!R && isRandomAccessRange!(typeof(r[])))
{
return choice(r[]);
}It should work for containers as well, such as |
No description provided.