constancy and template parameters in D2.009+

Christopher Wright dhasenan at gmail.com
Thu Dec 13 18:02:00 PST 2007


Janice Caron wrote:
> That template will remove ALL const, not just head const. You need
> something that transforms const(int[]) into const(int)[]. And it has
> to work for invariant too. (And even for const pointer to invariant -
> yes such things /are/ allowed to exist in D).
> 
> It's a much harder template to write than you might first assume.
> 
> ....which is why I like D2.008's default, in every regard but the
> syntax (and the fact that it has some bugs which mean it doesn't
> actually work as advertised in all cases).

Well, in that case, you need some way of expressing const data but 
mutable reference (exists in 2.008, maybe a library solution in 2.009, 
right?), and you need some solution for structs (a library solution for 
2.009 would take care of that).

Then your RemoveHeadConst template would do what you expect with arrays 
and classes, but structs...? A const(Vector!(int)) would turn into a 
Vector!(int) rather than a Vector!(const(int)). Is that what should 
happen? I think that depends on the type involved.

Library types will have to define ways to get head-mutable forms of 
things, I think. I also think it is a mess.

The convention will probably be along the lines of:
struct Name(Args...) {
    Name!(RemoveHeadConst!(Args[0]), Args[1], Args[2], ...) HeadMutable() {
       return typeof(return).init;
    }
}

That's more work than most people will want to put into it.



More information about the Digitalmars-d mailing list