<div class="gmail_quote">On Mon, Jun 28, 2010 at 10:56, Jacob Carlborg <span dir="ltr"><<a href="mailto:doob@me.com">doob@me.com</a>></span> wrote:<br><div><br>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'.<br>
<br>So your function can be done this way in D2:<br><br>void foo(T,U)(in T[] a, U b) if (is(U : Unqual!T)) // that compiles only if b can be cast to A<br>{<br> writeln(a,b);<br>}<br><br>"asd".foo('s'); // prints "asds".<br>
<br>is(U == Unqual!T) does not work, for U is 'char' while Unqual!T is
'dchar'.<br><br>More generally, using ranges and not arrays, the template becomes a bit more heavy:<br><br>void foo(Range,Elem)(in Range range, Elem elem) <br>if (isInputRange!Range && is(Elem : Unqual!(ElementType!Range)))<br>
{<br>...<br>}<br> <br><br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div>I don't think I understand what you're showing here. How would I strip off the const/immutable with a template ?<br>
</div><br></blockquote><div><br>Hmmm...<br>* plays with is expressions *<br><br>This seems to work:<br><br>template UnConst(T)<br>{<br> static if (is(T t == const U, U)) // that is: 'if T is a 'const U', for some U'<br>
alias U UnConst; // then give me the U, (ie, T without a const)<br> else<br> alias T UnConst; // else give me the (original) T<br>}<br><br>template UnImmutable(T)<br>
{<br> static if (is(T t == immutable U, U)) // 'if T is an 'immutable U', for some U'<br> alias U UnImmutable;<br> else<br> alias T UnImmutable;<br>}<br> <br>test:<br><br>void main() {<br>
alias const int Int;<br> writeln(UnConst!Int.stringof);<br> writeln(Int.stringof);<br> writeln(UnConst!int.stringof);<br> writeln(UnConst!(const int).stringof);<br> writeln(UnConst!(immutable int).stringof);<br>
<br> alias immutable int IInt;<br> writeln(UnConst!IInt.stringof);<br> writeln(IInt.stringof);<br> writeln(UnImmutable!int.stringof);<br> writeln(UnImmutable!(const int).stringof);<br> writeln(UnImmutable!(immutable int).stringof);<br>
}<br><br>Philippe<br><br></div></div>