The tailconst problem (and suggestions for solution)

Janice Caron caron800 at googlemail.com
Fri Dec 7 06:39:03 PST 2007


On Dec 7, 2007 2:14 PM, Don Clugston <dac at nospam.com.au> wrote:
> Don't you just need to wrap it in a struct ??
>
> struct A
> {
>    const B b;
> }
>
> A a;
> Then you can't change a.b, but you can replace a.
> Or did I miss something?

I thought the intent was that TailConst!(T) would be accessed exactly
like T or const(T). As in, you want to be able to write "a.member",
but access "b.member". As in:

    class T
    {
        int x;
        this() { x = 42; }
        int get() const { return x; }
        void set(int n) { x = n; }
    }

    T mt;
    TailConst!(T) tt;
    const(T) ct;

    // new
    mt = new T; // OK
    tt = new TailConst!(T); // OK
    ct = new const(T); // ERROR

    // reading
    int n;
    n = mt.get; // OK
    n = tt.get; // OK
    n = ct.get; // OK

    // writing
    mt.set(1); // OK
    tt.set(1); // ERROR
    ct.set(1); // ERROR

I think that wrapping it in a struct will work, providing we have
alias this. (Right now, we don't, so that didn't occur to me). As in

    struct tailConst(T)
    {
        const(T) __this;
        alias __this this;
    }

but without the alias this, you'd have to explicitly provide an extra
level of dereferencing. So yeah! Give us alias this and that will work
just fine. Problem solved!



More information about the Digitalmars-d mailing list