Why doesn't this work in D2?

Jacob Carlborg doob at me.com
Mon Jun 28 05:54:27 PDT 2010


On 2010-06-28 14:14, Philippe Sigaud wrote:
> On Mon, Jun 28, 2010 at 10:56, Jacob Carlborg <doob at me.com
> <mailto:doob at me.com>> wrote:
>
> Something to keep in mind: as of 2.04x (.045? maybe), the way UTF-8 /
> UTF-32 is managed was changed. "asd" is an array of immutable(dchar),
> not imutable(char). At least DMD tells me that its element type is 'dchar'.
>
> So your function can be done this way in D2:
>
> void foo(T,U)(in T[] a, U b) if (is(U : Unqual!T)) // that compiles only
> if b can be cast to A
> {
>      writeln(a,b);
> }
>
> "asd".foo('s'); // prints "asds".
>
> is(U == Unqual!T) does not work, for U is 'char' while Unqual!T is 'dchar'.
>
> More generally, using ranges and not arrays, the template becomes a bit
> more heavy:
>
> void foo(Range,Elem)(in Range range, Elem elem)
> if (isInputRange!Range && is(Elem : Unqual!(ElementType!Range)))
> {
> ...
> }
>
>
>     I don't think I understand what you're showing here. How would I
>     strip off the const/immutable with a template ?
>
>
> Hmmm...
> * plays with is expressions *
>
> This seems to work:
>
> template UnConst(T)
> {
>      static if (is(T t == const U, U)) // that is: 'if T is a 'const U',
> for some U'
>          alias U UnConst;                      // then give me the U,
> (ie, T without a const)
>      else
>          alias T UnConst;                      // else give me the
> (original) T
> }
>
> template UnImmutable(T)
> {
>      static if (is(T t == immutable U, U)) // 'if T is an 'immutable U',
> for some U'
>          alias U UnImmutable;
>      else
>          alias T UnImmutable;
> }
>
> test:
>
> void main() {
>      alias const int Int;
>      writeln(UnConst!Int.stringof);
>      writeln(Int.stringof);
>      writeln(UnConst!int.stringof);
>      writeln(UnConst!(const int).stringof);
>      writeln(UnConst!(immutable int).stringof);
>
>      alias immutable int IInt;
>      writeln(UnConst!IInt.stringof);
>      writeln(IInt.stringof);
>      writeln(UnImmutable!int.stringof);
>      writeln(UnImmutable!(const int).stringof);
>      writeln(UnImmutable!(immutable int).stringof);
> }
>
> Philippe

Hmm, now I don't know what I'm doing, I thought you could do something 
like this:

template Char (T)
{
	alias T Char;
}

void foo (T) (Char!(T) b)
{

}

void main ()
{
	foo('s');
}


-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list