From 403141166b624c47cafcb5904724b62271f63785 Mon Sep 17 00:00:00 2001 From: Basile Burg Date: Mon, 19 Mar 2018 20:28:22 +0100 Subject: [PATCH 1/2] fix issue 18631 - Allow choice to work with const arrays --- std/random.d | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/std/random.d b/std/random.d index c0cf7e13b43..98c02dea78b 100644 --- a/std/random.d +++ b/std/random.d @@ -2017,7 +2017,7 @@ Returns: return a `ref` to the $(D range element), otherwise it will return a copy. */ -auto ref choice(Range, RandomGen = Random)(auto ref Range range, +auto ref choice(Range, RandomGen = Random)(inout auto ref Range range, ref RandomGen urng) if (isRandomAccessRange!Range && hasLength!Range && isUniformRNG!RandomGen) { @@ -2055,6 +2055,13 @@ auto ref choice(Range)(auto ref Range range) "Choice did not return a valid element from the given Range"); } +@safe unittest // issue 18631 +{ + const a = [0,1,2]; + auto r = choice(a); + auto s = choice(cast(const)[0,1,2]); +} + @safe unittest { import std.algorithm.searching : canFind; From 8f2ae51cb8e6b99ab04c42631d1c71a650ee23f8 Mon Sep 17 00:00:00 2001 From: Basile Burg Date: Mon, 19 Mar 2018 21:23:21 +0100 Subject: [PATCH 2/2] proper usage of inout --- std/random.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/random.d b/std/random.d index 98c02dea78b..29a0b542013 100644 --- a/std/random.d +++ b/std/random.d @@ -2017,7 +2017,7 @@ Returns: return a `ref` to the $(D range element), otherwise it will return a copy. */ -auto ref choice(Range, RandomGen = Random)(inout auto ref Range range, +auto ref inout(ElementType!Range) choice(Range, RandomGen = Random)(inout auto ref Range range, ref RandomGen urng) if (isRandomAccessRange!Range && hasLength!Range && isUniformRNG!RandomGen) {