const again

Janice Caron caron800 at googlemail.com
Thu Dec 6 13:53:36 PST 2007


May I be the first to say I am very happy with everything you've said.
Thank you.
:-)


On 12/6/07, Walter Bright <newshound1 at digitalmars.com> wrote:
> There's one last problem:
>         class C { }
>         const(C)[] a;
>         a[3] = new C();  // error, x[3] is const
> does not work with this new regime. Every twist we tried to make it work
> caused other problems.

I understand the problem. Have you considered the possible solution
which has been suggested on this group - a special syntax for classes
having mutable refs to constant data? Under that suggestion, the above
code would become:

        class C { }
        const(C)&[] a;
        a[3] = new C();  // OK

It's the ampersand that makes it OK. It tells you that the reference
is not constified.

I think that would work, in exactly the same way that

        struct S { }
        struct(S)*[] a;
        a[3] = new S();  // OK

works.

> Andrei also mentioned the
> possibility of using a template:
>         TailConst!(C)[] a;
> which would do whatever was necessary under the hood to allow the
> elements of a to be rebound while still keeping the contents of the C
> objects const.

That's kind of the same thing as const(C)&[] a, except with a template
instead of an ampersand. Up to you which way you like more.

Anyway, like I said, I'm happy.

D is brilliant :-)



More information about the Digitalmars-d mailing list