These templates seem to be hard to get right. I guess with experience they get easier to write and comprehend. Anyways, thanks for the explanation.<br><br><div class="gmail_quote">On Wed, Jul 28, 2010 at 4:44 PM, Philippe Sigaud <span dir="ltr"><<a href="mailto:philippe.sigaud@gmail.com">philippe.sigaud@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="gmail_quote">On Wed, Jul 28, 2010 at 05:50, Andrej Mitrovic <span dir="ltr"><<a href="mailto:andrej.mitrovich@gmail.com" target="_blank">andrej.mitrovich@gmail.com</a>></span> wrote:<br>
<div><br></div><div>I won't comment on the double/float issue, I do not anything about it.</div>
<div> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">T[] find(T, E)(T[] haystack, E needle)<br>
if (is(typeof(haystack[0] != needle) == bool))<br><br></div><div class="im">
T1[] find(T1, T2)(T1[] longer, T2[] shorter)<br>
if (is(typeof(longer[0 .. 1] == shorter) : bool))<br><br></div><div class="im">
Also, Andrei, you never explained the if(is()) signature of the second templated function. I hope to get some pointers on that. :)<br>
</div></blockquote></div><div><br></div>I'll have a try at this one.<div>As you may know, the is(typeof()) syntax is a way to try to compile an expression and see if it works. If it works, it has a type, given by typeof and is(Type) returns true. So the first one is really saying: "Hey compiler, may I compare an element of haystack (of type T) with an E</div>
<div>In this case, longer is an array. So longer[0] would be an element, a T1. There is no chance that shorter will match as an element cannot be equal to an array ... except, of course, if longer is an array of T2[] (T1 == T2[]). Longer is a T2[][]. That may happen for arrays of strings, strings being arrays of chars. And I think that's the problem you have in your last unit test.<div>
<br></div><div>Anyway, he takes longer[0..1] to get a slice, which is a dynamic array, a T1[] and hence comparable to a T2[]... most of the time.</div><div><br></div><div>As for using ': bool' instead of '== bool' as in the first find(), I don't think there is any difference in this case. I understand T == U as 'T is the exact same type as U', whereas T : U is for me "T is a subtype of U". But then, a subtype of bool is pretty much constrained to be a bool. The T : U syntax is pretty much only used for classes : if(is(T : MyClass)) is saying : 'compile this only if T is a subclass of MyClass'.</div>
<div><br></div><div><br></div><div>Philippe</div><div><br></div><div><br></div><div><br></div><div>, except if longer is in fact</div></div>
</blockquote></div><br>