On Thursday, 20 March 2014 at 18:43:49 UTC, Chris Williams wrote:
That's only completely true if structs are referred to by pointer. ref parameters/returns aren't quite sufficient to keep a struct acting as a reference for all purposes.


As far as I can tell, you're thinking of _passing_ struct parameters, and here, indeed, passing by ref is sufficient.

The problem comes when you want to _store_ them. It's not safe to just store a pointer, because the (value type) struct that's being pointed to might go out of scope and be deleted.

However, you can make structs behave like reference types behave like reference types, simply by making them contain (safe) references to the actual data they contain. E.g. (stupidly simple example):

    struct Foo
    {
      private:
        int *_a;

      public:
        this(int val)
        {
            _a = new int;
            *_a = val;
        }

        ref int a() @property
        {
            return *_a;
        }
    }

    unittest
    {
        auto foo1 = Foo(23);
        auto foo2 = foo1;

        foo2.a = 4;

        writeln(foo1.a);
    }

Most of the discussion over RNGs in the last year is about whether we need to take a (more sophisticated) variant of this kind of approach to implement reference-type semantics for RNGs, or whether we should do something different. std.random2 is ... something different ;-)

Reply via email to