Page 149, the two shorter-hand ways of writing function literals are not working for me:<br><br>void main() { }<br><br>T[] find(alias pred, T)(T[] input)<br> if (is(typeof(pred(input[0])) == bool))<br>{<br> for (; input.length > 0; input = input[1 .. $]) {<br>
if (pred(input[0]))<br> break;<br> }<br> return input;<br>}<br><br>unittest {<br> int[] a = [1, 2, 3, 4, -5, 3, -4];<br> <br> auto b = find!(function bool(int x) { return x < 0; })(a);<br>
//~ auto c = find!(function(x) { return x < 0; })(a);<br> //~ auto d = find!((x) { return x < 0; })(a);<br>}<br><br>The first one works fine. But if I uncomment the second or third line I get these errors:<br>
<br>higher_order_functions.d(17): Error: template higher_order_functions.find(alias pred,T) if (is(typeof(pred(input[0])) == bool)) does not match any function template declaration<br><br>higher_order_functions.d(17): Error: template higher_order_functions.find(alias pred,T) if (is(typeof(pred(input[0])) == bool)) cannot deduce template function from argument types !(__funcliteral1)(int[])<br>
<br><br><div class="gmail_quote">On Wed, Jul 28, 2010 at 10:44 PM, Andrej Mitrovic <span dir="ltr"><<a href="mailto:andrej.mitrovich@gmail.com">andrej.mitrovich@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;">
On page 143 there's a new find() overloaded function which is more specialized than the last generic function, but I'm still getting an ambiguity error. Although the book doesn't state if that specific unittest should work, so I'm left wondering :) Here's the code:<br>
<br>void main()<div class="im"><br>{<br>}<br><br>T1[] find(T1, T2)(T1[] longer, T2[] shorter)<br> if (is(typeof(longer[0 .. 1] == shorter) : bool))<br>{<br> while (longer.length >= shorter.length) {<br> if (longer[0 .. shorter.length] == shorter)<br>
break;<br> longer = longer[1 .. $];<br> }<br> return longer;<br>}<br><br></div>int[] find(int[] longer, int[] shorter) { }<br><br>unittest {<br> int[] ints1 = [1, 2, 3, 5, 2] ;<br> int[] ints2 = [3, 5];<br>
auto test = find(ints1, ints2);<br>}<br><br>test.d(16): Error: function test.find conflicts with template test.find(T1,T2) if (is(typeof(longer[0..1] == shorter) : bool)) at test.d(5)<br><br><br>And on page 145 the last example is:<br>
<br>void main() { }<br><br>void transmogrify(uint) { }<br>void transmogrify(long) { }<br>void transmogrify(T)(T value) { }<br><br>unittest {<br> transmogrify(42);<br> transmogrify("hello");<br> transmogrify(1.1);<br>
}<br><br>test2.d(5): Error: template test2.transmogrify(T) conflicts with function test2.transmogrify at test2.d(3)<div><div></div><div class="h5"><br><br><div class="gmail_quote">On Wed, Jul 28, 2010 at 7:32 PM, Andrej Mitrovic <span dir="ltr"><<a href="mailto:andrej.mitrovich@gmail.com" target="_blank">andrej.mitrovich@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;">Woops, I got confused. I was thinking about structs, not arrays. <br><br>But yeah, in this case a float gets compared to a double and it seems to evaluate to false.<div>
<div></div><div><br><br><div class="gmail_quote">On Wed, Jul 28, 2010 at 7:26 PM, Jonathan M Davis <span dir="ltr"><<a href="mailto:jmdavisprog@gmail.com" target="_blank">jmdavisprog@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>On Wednesday, July 28, 2010 09:49:52 Andrej Mitrovic wrote:<br>
> IIRC arrays are value types and as such they are compared bit-by-bit<br>
> to each other, right?<br>
<br>
</div>Dynamic arrays are reference types (though static arrays are indeed value<br>
types), and they evaluate for equality by comparing each element in turn (though<br>
presumably, they compare their length first and don't bother with element<br>
comparisons if the lengths don't match). Look at section 4.1.5 on page 100 of<br>
TDPL.<br>
<font color="#888888"><br>
- Jonathan M Davis<br>
</font></blockquote></div><br>
</div></div></blockquote></div><br>
</div></div></blockquote></div><br>