On Tuesday, 18 June 2013 at 14:39:38 UTC, Andrei Alexandrescu wrote:
On 6/18/13 10:16 AM, H. S. Teoh wrote:
I say again that RNGs being passed by value is a major BUG. The above situation is a prime example of this problem. We *need* to make RNGs passed by reference. For situations where you *want* to duplicate a pseudo random sequence, an explicit method should be provided to clone
the RNG. Duplication of RNGs should never be implicit.

Occasionally copying a RNG's state is useful, but I agree most of the time you want to just take a reference to it.

I think a good way toward a solution is http://d.puremagic.com/issues/show_bug.cgi?id=10404.


Andrei

That sounds nice, but is it possible? The only way to truly forward everything is via an alias this. However, while all the calls are properly forwarded, it tends to fail for "is" traits. EG:

struct S
{
    int s;

    alias INT = int;

    void foo()
    {writeln("foo()");}

    void foo(int)
    {writeln("foo(int)");}

    void foo2(Args)(int)
    {writeln("foo!" ~ Args.stringof ~ "(int)");}

    @property bool empty(){return true;}
    @property int front(){return 1;}
    void popFront(){}
}

class Class(S)
{
    private S __s;
    this(Args...)(auto ref Args args){__s = S(args);}

    alias __s this;
}

void main()
{
    auto c = new Class!S(5);
    c.foo();
    c.foo(5);
    c.foo2!short(5);
    c.s = 5; //it's a trap!
    Class!S.INT a = 5;

    assert(isInputRange!S);
    assert(isInputRange!(Class!S));
}

Here, this compiles, but fails at the last line... Or maybe I'm not creative enough... ?

Reply via email to