Thread overview
[Issue 18631] std.random.choice does not work with const arrays
Mar 19, 2018
Basile B.
Mar 23, 2018
Jonathan M Davis
Feb 23, 2022
Paul Backus
Jul 05, 2022
Dlang Bot
Jul 07, 2022
Dlang Bot
March 19, 2018
https://issues.dlang.org/show_bug.cgi?id=18631

Basile B. <b2.temp@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Hardware|x86                         |All
                 OS|Windows                     |All
           Severity|enhancement                 |normal

--
March 20, 2018
https://issues.dlang.org/show_bug.cgi?id=18631

--- Comment #1 from github-bugzilla@puremagic.com ---
Commits pushed to master at https://github.com/dlang/phobos

https://github.com/dlang/phobos/commit/403141166b624c47cafcb5904724b62271f63785 fix issue 18631 - Allow choice to work with const arrays

https://github.com/dlang/phobos/commit/b99915397b376cde180f6ef3cc187e0046a97422 Merge pull request #6302 from BBasile/choice-const-rndrange

fix issue 18631 - Allow choice to work with const arrays

--
March 20, 2018
https://issues.dlang.org/show_bug.cgi?id=18631

github-bugzilla@puremagic.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--
March 23, 2018
https://issues.dlang.org/show_bug.cgi?id=18631

Jonathan M Davis <issues.dlang@jmdavisProg.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |issues.dlang@jmdavisProg.co
                   |                            |m
         Resolution|FIXED                       |---

--- Comment #2 from Jonathan M Davis <issues.dlang@jmdavisProg.com> ---
The fix that was merged broke code and has been reverted: https://github.com/dlang/phobos/pull/6331

A different solution is required.

--
February 23, 2022
https://issues.dlang.org/show_bug.cgi?id=18631

Paul Backus <snarwin+bugzilla@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
                 CC|                            |snarwin+bugzilla@gmail.com
         Resolution|---                         |DUPLICATE

--- Comment #3 from Paul Backus <snarwin+bugzilla@gmail.com> ---
This is a special case of a general problem with ranges and const. A const object cannot implement popFront (which mutates its receiver), and therefore cannot be a range.

In this case, the problem can be worked around by using the [] operator to obtain a mutable slice over the const array:

---
const x = [1, 2];
auto y = choice(x[]);
---

*** This issue has been marked as a duplicate of issue 17771 ***

--
July 04, 2022
https://issues.dlang.org/show_bug.cgi?id=18631

Steven Schveighoffer <schveiguy@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |schveiguy@gmail.com
         Resolution|DUPLICATE                   |---

--- Comment #4 from Steven Schveighoffer <schveiguy@gmail.com> ---
This is not a duplicate of the general foreach problem. It's specific to choice.

Other range-accepting functions work with const arrays just fine. This is a problem with choice.

The fix is to remove the `auto ref` modifier on the range, which is nonsensical, since it doesn't actually modify the range at all. It probably was a premature optimization (verified it has been there from the start).

--
July 05, 2022
https://issues.dlang.org/show_bug.cgi?id=18631

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #5 from Dlang Bot <dlang-bot@dlang.rocks> ---
@GrimMaple created dlang/phobos pull request #8495 "Fix Issue 18631 - std.random.choice does not work with const arrays" fixing this issue:

- Fix Issue 18631 - std.random.choice does not work with const arrays

  `auto ref` is not needed on `Range` param

https://github.com/dlang/phobos/pull/8495

--
July 07, 2022
https://issues.dlang.org/show_bug.cgi?id=18631

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #6 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/phobos pull request #8495 "Fix Issue 18631 - std.random.choice does not work with const arrays" was merged into master:

- ee474f0cfc543e844bcbcf96a0da17fc3bf8c8f4 by Grim Maple:
  Fix Issue 18631 - std.random.choice does not work with const arrays

  `auto ref` is not needed on `Range` param

https://github.com/dlang/phobos/pull/8495

--