Stripping away const/invariant in D 2.0

Daniel Keep daniel.keep.lists at gmail.com
Tue Aug 28 01:58:06 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).

This came up because of my templated join function which takes an array
of arrays, a separator, and joins all the arrays together.  For instance:

  ["foo","bar","baz"].join(", ") == "foo, bar, baz"

The problem is when the arguments are some combination of const and/or
invariant.  In order to efficiently do the join, it allocates a result
array large enough for the full result, then fills it in using slicing.
 Problem is, what type does it use?

Given:

  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.  I've tried is( T V : const V ) and is( T V == const V ) as well as
various array tricks, but nothing seems to be working.  In the two "is"
cases, V is *always* the same type as T.

So unless I've completely missed something, I think one of the following
needs to be added:

1. Support for is( T U == const ) and is( T U == invariant ), where U
   becomes a mutable version of T,

2. a new __traits form: __traits(mutableTypeOf, T) or

3. a new `mutable` keyword that works analogously to const and
   invariant, except that it strips off const and invariant.

I also feel that these constructs should do this constness stripping
*only* one level deep (so mutable const(char)[][] is (const(char)[])[]
-- removing all levels of constness can be done using a recursive template.)

Thoughts (or maybe even corrections? :) )

	-- Daniel



More information about the Digitalmars-d mailing list