Latest const expansion

Bill Baxter dnewsgroup at billbaxter.com
Tue Sep 11 14:41:26 PDT 2007


Regan Heath wrote:
> const(T)  indicates both head/tail const
> const(*T) indicates tail const
> const(&T) indicates head const

I think this befuddles the nice clean idea of const(...) as a type 
constructor that takes the type inside the parens and makes a new 
const-ified type out of it.

But fortunately that cleanness can be reclaimed by thinking instead of a 
_family_ of type constructors, which do head/tail/full const of their 
argument.  In short, just move the symbol out of the parens so the thing 
inside the parens remains a valid type.

const(T) - type constructor making both head/tail const version of T
const*(T) - type constructor making tail const version of T
const**(T) - type constructor making tail-of-tail const version of T

[..and now we're back to what I think was Janice's suggestion many many 
posts ago, which I liked then too.]

I think this is quite logical.  The const*() type constructor can be 
thought of as dereferencing the type in parens by one level and *then* 
applying the constness to the result, and then returning a reference to 
that type.
For example const*(int*):
     dereference int*  to get --> int
     apply const to get --> const(int)
     address-of to get --> const(int)*

So in this case it's the same as const(int)*.
But for a class, const*(C), it lets you do something that can't be done 
by peeling off the pointer.

That's tail const.

I'm not so sure I like using const&(T) to mean head const, though.  It 
doesn't really seem right for some reason.

Using the * for tail has an interpretation in terms of performing an 
actual dereference on an object, then applying full const.  But const&() 
doesn't really have such an interpretation. You're not taking the 
address and then applying full const.  So for that reason another 
keyword is preferable I think.  Like 'final'.  Or maybe headconst or 
'hconst'.

--bb



More information about the Digitalmars-d mailing list