Stripping away const/invariant in D 2.0

Daniel Keep daniel.keep.lists at gmail.com
Tue Aug 28 04:36:58 PDT 2007



Christian Kamm wrote:
>>> I've been trying to work this out for a few hours now, and I'm drawing a
>>> blank.  In D 2.0, there doesn't appear to be any way of deriving the
>>> type of T given either (const T) or (invariant T).
>> Use "typeof(T)". For example if T is invariant char , then typeof(T) ==
>> char. typeof() kinda works like a declaration, so it removes the top
>> level const/invariant.
> 
> This works for plan data like char (since const(char) really is the same
> type as char, if I understand it right - there's no 'tail' that could be
> const), but not for reference types:
> 
> class T {}
> const(T) var;
> writefln(typeid(typeof(var)));
> produces
> const T
> 
> However, I think templates are broken with regard to const, since:
> 
> class T {}
> const(T) var;
> pragma(msg, typeof(var));
> errors with
> consttest.d(14): Error: string expected for message, not 'const T'
> 
> and
> 
> template Foo(Q) {
>   pragma(msg, Q);
> }
> 
> class T {}
> const(T) var;
> Foo!(typeof(var));
> errors with
> consttest.d(7): pragma msg string expected for message, not 'T'
> 

Shouldn't those be typeof(var).stringof and Q.stringof?

	-- Daniel



More information about the Digitalmars-d mailing list