Stripping away const/invariant in D 2.0

Stewart Gordon smjg_1998 at yahoo.com
Tue Aug 28 07:38:52 PDT 2007


"Daniel Keep" <daniel.keep.lists at gmail.com> wrote in message 
news:fb0o30$2fd4$1 at digitalmars.com...
> 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).
<snip>
>  T[] join(T,U)(in T[][] parts, in U[] sep=null)
>
> There doesn't appear to be any way to derive a mutable version of T or
> U.

Here's some code that works:

----------
template unconst(T) {
    pragma(msg, "unconst(" ~ T.stringof ~ ")");
    alias unc!(T).u unconst;
    pragma(msg, "unconst(" ~ T.stringof ~ ") -> " ~ unconst.stringof);
}

template unc(T) {
    T dummy;
    alias typeof(dummy) u;
}

template unc(T : T[]) {
    alias unc!(T).u[] u;
}

alias int[] arrayType1;
alias const(int)[] arrayType2;
alias invariant(int)[] arrayType3;

alias const(int[]) arrayType4;
alias const(const(int)[]) arrayType5;
alias const(invariant(int)[]) arrayType6;

alias invariant(int[]) arrayType7;
alias invariant(const(int)[]) arrayType8;
alias invariant(invariant(int)[]) arrayType9;

unconst!(arrayType1) array1;
static assert (is(typeof(array1) == int[]));
unconst!(arrayType2) array2;
static assert (is(typeof(array2) == int[]));
unconst!(arrayType3) array3;
static assert (is(typeof(array3) == int[]));
unconst!(arrayType4) array4;
static assert (is(typeof(array4) == int[]));
unconst!(arrayType5) array5;
static assert (is(typeof(array5) == int[]));
unconst!(arrayType6) array6;
static assert (is(typeof(array6) == int[]));
unconst!(arrayType7) array7;
static assert (is(typeof(array7) == int[]));
unconst!(arrayType8) array8;
static assert (is(typeof(array8) == int[]));
unconst!(arrayType9) array9;
static assert (is(typeof(array9) == int[]));
----------

Stewart. 




More information about the Digitalmars-d mailing list