January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #10 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-01-17 17:43:00 PST ---
(In reply to comment #9)
> If you write "static arr = [EnumMembers!T];", you should be able to evade issue 6057.

The issue is with unittests.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #11 from hsteoh@quickfur.ath.cx 2013-01-17 21:15:26 PST ---
I don't understand. If you use that line in uniform(), and it works, then unittests shouldn't have any problems either, no?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #12 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-01-17 21:21:50 PST ---
(In reply to comment #11)
> I don't understand. If you use that line in uniform(), and it works, then unittests shouldn't have any problems either, no?

The problem is the enum has to be hidden in a unittest block like so:

version(unittest)
{
   enum TestEnum { ... }
}

unittest
{
    foreach (_; 0 .. 100)
        assert(uniform!TestEnum() == ...);
}

And this causes linking problems due to Issue 6057.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #13 from hsteoh@quickfur.ath.cx 2013-01-17 21:35:46 PST ---
Oh? This code compiles & links just fine:

import std.random;
import std.traits;

E randomPick(E)() if (is(E == enum)) {
        static members = [ EnumMembers!E ];
        return members[uniform(0, EnumMembers!E.length)];
}

void main() {
}

unittest {
        enum Fruit { Apple = 12, Mango = 29, Pear = 72 };
        foreach (_; 0 .. 100) {
                auto f = randomPick!Fruit();
                assert(f == Fruit.Apple || f == Fruit.Mango || f ==
Fruit.Pear);
        }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #14 from bearophile_hugs@eml.cc 2013-01-18 10:20:09 PST ---
(In reply to comment #6)

> > T uniform(T)()
> > if (is(T == enum) && isIntegral!T || isSomeChar!T)
> > {
> >     static immutable T[EnumMembers!T.length] members = [EnumMembers!T];
> >     return members[std.random.uniform(0, members.length)];
> > }
> 
> That's not doing what was requested.

Then I don't understand. This ER asks for that function overload to return a "random enum member". Isn't members[std.random.uniform(0, members.length)] a random enum member?

And beside what the OP is asking, uniform() returns single random values of a type. Isn't this what I am doing there?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #15 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-01-18 10:30:47 PST ---
(In reply to comment #14)
> (In reply to comment #6)
> 
> > > T uniform(T)()
> > > if (is(T == enum) && isIntegral!T || isSomeChar!T)
> > > {
> > >     static immutable T[EnumMembers!T.length] members = [EnumMembers!T];
> > >     return members[std.random.uniform(0, members.length)];
> > > }
> > 
> > That's not doing what was requested.
> 
> Then I don't understand.

I've misread the last statement (it was late) as:

return std.random.uniform(0, members.length);

instead of:

return members[std.random.uniform(0, members.length)];

Apologies.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 22, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #16 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-01-21 17:01:19 PST ---
https://github.com/D-Programming-Language/phobos/pull/1086

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 22, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #17 from bearophile_hugs@eml.cc 2013-01-21 17:10:03 PST ---
(In reply to comment #16)
> https://github.com/D-Programming-Language/phobos/pull/1086

I have seen that of this line you have dropped both the immutable and fixed sized array, can you explain why a dynamic array is better than a fixed array in the static segment?

static immutable T[EnumMembers!T.length] members = [EnumMembers!T];

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 22, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #18 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-01-21 17:21:07 PST ---
(In reply to comment #17)
> (In reply to comment #16)
> > https://github.com/D-Programming-Language/phobos/pull/1086
> 
> I have seen that of this line you have dropped both the immutable and fixed sized array, can you explain why a dynamic array is better than a fixed array in the static segment?
> 
> static immutable T[EnumMembers!T.length] members = [EnumMembers!T];

It was a mistake, I've updated the code now.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 13, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9339



--- Comment #19 from github-bugzilla@puremagic.com 2013-02-12 17:25:07 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/c95100ba78cc137ee4af37d59e3b3dfb704a4832 Fixes Issue 9339 - Uniform for enums.

https://github.com/D-Programming-Language/phobos/commit/6a3ffa5e136d22b31529e6a3688cb8ce3a5508a0 Merge pull request #1086 from AndrejMitrovic/Fix9339

Issue 9339 - std.random.uniform for enums

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------