The Status of Const

Tomek Sowiński just at ask.me
Fri Aug 13 12:46:30 PDT 2010


Graham St Jack napisał:

> The major language hassle for me is that a class reference has the same
> const/immutable/shared type as the object is refers to, unlike pointers.
> I know that Rebindable is a workaround, but I don't find it satisfactory
> - essentially it uses brute-force casting to defeat the type system, and
> isn't syntactically identical to the bound type.
> 
> [snip]
> 
> Is there any plan to introduce some way of having a mutable reference to
> an immutable class object on the heap? Do others see this as a problem
> at all?

I see it as a huge problem too. Crazy idea: introduce @tail to be used in combo with const/immutable. I'd find it even useful on structs.

@tail immutable struct Ref {
    int[] array;
    int* pointer;
    int value;
}

This would be the same as:

struct Ref {
    immutable(int)[] array;
    immutable(int)* pointer;
    int value;
}

No need to examine types field by field to impose such a popular constraint on your structure. But the real money comes from finally 
having a language-legal way to express a mutable reference to an immutable class.

Not to mention that backwards-compatibility is kept (no book will suffer).


Tomek


More information about the Digitalmars-d mailing list