compiler error when trying to get random key from AA
mark
mark at qtrac.eu
Sat Jan 25 09:06:53 UTC 2020
On Saturday, 25 January 2020 at 08:59:23 UTC, Basile B. wrote:
> On Saturday, 25 January 2020 at 08:35:18 UTC, mark wrote:
>> I have this code:
>>
>> import std.random;
>> import std.stdio;
>> void main()
>> {
>> auto aa = ["one": 1, "two": 2, "three": 3];
>> writeln(aa);
>> auto rnd = rndGen;
>> auto word = aa.byKey.choice(rnd);
>> writeln(word);
>> }
>>
>> And in the D playground it gives this error:
[snip]
>> I am treating aa as a set and want to pick a random word from
>> it.
>> What am I doing wrong?
>>
>> I'm sorry I can't give a link to this code in the D playground
>> but the URL in the web browser is just https://run.dlang.io/
>> and when I click Shorten to get a URL nothing seems to happen
>> (using Firefox on Linux).
>
> rndGen is a range.
>
> Use `auto word = aa.byKey.choice(rnd.front())` as index instead.
> Then `rndGen.popFront()` to advance.
I tried that. It doesn't solve the problem but does reduce the
size of the error output to:
onlineapp.d(9): Error: template std.random.choice cannot deduce
function from argument types !()(Result, uint), candidates are:
/dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2599):
choice(Range, RandomGen = Random)(auto ref Range range, ref
RandomGen urng)
/dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2609):
choice(Range)(auto ref Range range)
More information about the Digitalmars-d-learn
mailing list