TDPL: Overloading template functions
Philippe Sigaud
philippe.sigaud at gmail.com
Fri Jul 30 03:11:28 PDT 2010
On Thu, Jul 29, 2010 at 23:28, Andrej Mitrovic
<andrej.mitrovich at gmail.com>wrote:
> How do I print out an expanded tuple but with spaces between the values?
> There's this example on page 164:
>
> import std.typecons, std.stdio;
>
> void fun(T...)(T args) {
> // create a tuple to pack all arguments together
> gun(tuple(args));
> }
>
> void gun(T)(T value) {
> // expand the tuple back
> writeln(value.expand);
> }
>
> void main() {
> fun(1);
> fun(1, 2.2);
> }
>
> This prints out:
> 1
> 12.2
>
> But that's confusing, since the values are "glued" together when printed
> out. I really want to print out:
> 1
> 1 2.2
>
> Any ideas?
>
You can iterate on the values and create the corresponding string. Note that
you must have a polymorphic function to map on a tuple's elements.
void gun(T)(T value)
{
string result;
foreach(index, Type; value.expand)
{
result ~= to!string(value.expand[i]) ~ " ";
}
writeln(result);
}
There will be a surnumerary " " 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.
>
> One other thing. I can use the .length property for value and parameter
> tuples, but only if I haven'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.
>
Yes, std.typecons.Tuple should have a length, it's trivial to add. It's
issue #4381
http://d.puremagic.com/issues/show_bug.cgi?id=4381
And yes, there are 'raw', compiler-managed tuples which are lists of types
and values 'glued' 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.
But, being a bunch of many different types, they cannot be returned by a
function. That's C inheritance for you. They have no .init value neither,
which I found too bad for generic code.
http://d.puremagic.com/issues/show_bug.cgi?id=4536
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.
Tuple!("index", int, "sum", double) t = tuple(1,3.14);
assert(t.index== 1); // t has .first and .second as members.
assert(t.sum== 3.14);
>
> It's a bit of a shame that there isn't a chapter on tuples except this
> brief mention (I don't see it listed in the contents page). I guess I'll
> take a look at the implementation.
>
>
Andrei didn'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.
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.
Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100730/38183700/attachment.html>
More information about the Digitalmars-d
mailing list