Stripping away const/invariant in D 2.0
Bruno Medeiros
brunodomedeiros+spam at com.gmail
Tue Aug 28 03:22:23 PDT 2007
Daniel Keep 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).
>
> 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.
>
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.
--
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
More information about the Digitalmars-d
mailing list