I kind of miss reference values on stack, so I attempted to make one in a struct. Pointers are pretty good (since d doesn't have ->), but it would be nice to avoid dereferencing them explicitly on assignment.

Since reference is a pointer that you can't change afterwards.
I tried something like this:

struct RefVal( T )
{
    this( T* val )                      {   ptr = val;  }

ref auto opAssign( T value ) { *ptr = value; return *ptr; } ref auto opAssign( ref T value ) { *ptr = value; return *ptr; }

    alias ptr this;
    T* ptr;
}

This works for most basic cases but breaks in:

struct Foo
{
    this( int k )
    {
        a = k;
    }

    void opAssign( int k )
    {
        a = k;
    }

    int a;
}

Foo foo = Foo(2);
Foo baz = Foo(3);
RefVal!Foo bar = RefVal!Foo( &foo );

bar = baz;

bar = 5; // Ooops! doesn't work

Is there a way to transparently pass everything to *RefVal.ptr?

Also is there a way to make "alias ptr this" to work with "private T*"? Ideally I wouldn't want to give access to the ptr, but for now it's handy as a workaround.

Reply via email to