Stripping away const/invariant in D 2.0
Christian Kamm
kamm.incasoftware at shift-at-left-and-remove-this.de
Tue Aug 28 04:15:09 PDT 2007
>> 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'
More information about the Digitalmars-d
mailing list