Thanks Phillippe, that explains a lot.<br><br>I like to try out all the examples, I never take any of it for granted. But hardly any book comes with 100% working examples, even the much praised CPL is loaded with syntax mistakes. With that said I have to admit I&#39;m really enjoying reading TDPL so far.<br>
<br><div class="gmail_quote">
On Fri, Jul 30, 2010 at 12:11 PM, Philippe Sigaud <span dir="ltr">&lt;<a href="mailto:philippe.sigaud@gmail.com" target="_blank">philippe.sigaud@gmail.com</a>&gt;</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"><div>On Thu, Jul 29, 2010 at 23:28, Andrej Mitrovic <span dir="ltr">&lt;<a href="mailto:andrej.mitrovich@gmail.com" target="_blank">andrej.mitrovich@gmail.com</a>&gt;</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;">


How do I print out an expanded tuple but with spaces between the values? There&#39;s this example on page 164:<br><br>import std.typecons, std.stdio;<br><br>void fun(T...)(T args) {<br>    // create a tuple to pack all arguments together<br>



    gun(tuple(args));<br>}<br><br>void gun(T)(T value) {                       <br>    // expand the tuple back<br>    writeln(value.expand);<br>}<br><br>void main() {<br>    fun(1);<br>    fun(1, 2.2);<br>}<br><br>This prints out:<br>



1<br>12.2<br><br>But that&#39;s confusing, since the values are &quot;glued&quot; together when printed out. I really want to print out:<br>1<br>1 2.2<br><br>Any ideas?<br></blockquote><div><br></div></div><div>You can iterate on the values and create the corresponding string. Note that you must have a polymorphic function to map on a tuple&#39;s elements.</div>

<div>
<div><br></div><div>void gun(T)(T value)</div><div>{</div></div><div>    string result;</div><div>    foreach(index, Type; value.expand)</div><div>    {</div><div>        result ~= to!string(value.expand[i]) ~ &quot;  &quot;;</div>


<div>    }</div><div>    writeln(result);</div><div>}</div><div><br></div><div>There will be a surnumerary &quot; &quot; at the very end, but I prefer to show the way to iterate on a tuple. value is a std.typecons.Tuple, but value.expand gives access to the raw (T...) tuple inside it.</div>

<div>
<div><br></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;"><br>One other thing. I can use the .length property for value and parameter tuples, but only if I haven&#39;t built them myself with the call to std.typecons.tuple(). For example I can call writeln(T.length) in the fun() function (where the compiler automatically constructs a tuple), but not in the gun() function. So there seems to be compiler tuples and user-constructed tuples, which are not the same. <br>


</blockquote><div><br></div></div><div>Yes, std.typecons.Tuple should have a length, it&#39;s trivial to add. It&#39;s issue #4381</div><div><br></div><div><a href="http://d.puremagic.com/issues/show_bug.cgi?id=4381" target="_blank">http://d.puremagic.com/issues/show_bug.cgi?id=4381</a></div>


<div><br></div><div>And yes, there are &#39;raw&#39;, compiler-managed tuples which are lists of types and values &#39;glued&#39; together. They are powerful, iterable, indexable, slicable, know their length, etc. They may even be assignable. Ah, in fact, they can hold anything that can be passed as a template argument, I guess.</div>


<div>But, being a bunch of many different types, they cannot be returned by a function. That&#39;s C inheritance for you. They have no .init value neither, which I found too bad for generic code.</div><div><br></div><div>


<a href="http://d.puremagic.com/issues/show_bug.cgi?id=4536" target="_blank">http://d.puremagic.com/issues/show_bug.cgi?id=4536</a></div><div><br></div><div>So std.typecons.Tuple provides a way to wrap values inside a struct, while giving access to the types and values, and that can be returned by a function. It can also have named members, which can be quite handy when you return many things from a function, grouped inside a tuple.</div>


<div><br></div><div>Tuple!(&quot;index&quot;, int, &quot;sum&quot;, double) t = tuple(1,3.14);</div><div><br></div><div>assert(t.index== 1); // t has .first and .second as members.</div><div>assert(t.sum== 3.14);</div><div>

<div>
<br></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;">
<br>It&#39;s a bit of a shame that there isn&#39;t a chapter on tuples except this brief mention (I don&#39;t see it listed in the contents page). I guess I&#39;ll take a look at the implementation.<div><div></div><div>
<br></div></div></blockquote><div><br></div></div><div>Andrei didn&#39;t want to talk too much about Phobos, as it was (ans still is!) in flux while he was writing this. D the language will not change much for some time, while the standard library is being actively transformed, if only to take into account the new features that were added for the past 6 months.</div>


<div><br></div><div>btw, I like you posts, but do not always have the time to answer them. If among the numerous issues you posted there there is still one that bother you, do not hesitate to ask again for an answer.</div>


<div><br></div><font color="#888888"><div> Philippe</div></font></div>
</blockquote></div><br>