The Status of Const

Tomek Sowiński just at ask.me
Mon Aug 16 15:12:35 PDT 2010


Dnia 16-08-2010 o 17:17:32 Steven Schveighoffer <schveiguy at yahoo.com>  
napisał(a):

> * Perhaps a good starting point is to look at std.container.SList and  
> see how const(SList!T) can be manipulated.
>  We can start simpler than that.  Give a struct:
>  struct Array(V)
> {
>   uint length;
>   V *v;
> }
>  How can we turn Array!V into Array!(const(V)) implicitly...
>  This will duplicate array tail-const, and be essential to making  
> tail-const ranges.
>  It should be a good starting test for any library/compiler solution.

Nice litmus test. I thought about it for a while and came up with:

Array!int arr = ... ;
Rebindable!(const Array!V) r = arr;

It actually compiles if you remove the template constraint off Rebindable.  
But tail const/immutable is generally desired so you'd end up with  
rebindable everywhere making variable manipulation cumbersome, the code  
less auditable, giving easy counter-arguments in language wars ("D must  
resort to casts, so much for the type system revolution, haha"), etc.

Plus, there's the merriness with shared (tail-shared structures, anyone?  
and of course you'd want a tail-const view on it at some point, etc).  
Rebindable (this name doesn't scale) would basically have to reimplement  
all of D's type conversions via operator overloading, template  
constraints, static ifs, and blunt casts. Like as if implementing all this  
in a compiler alone wasn't hard enough...


Tomek


More information about the Digitalmars-d mailing list