TDPL: Overloading template functions

Philippe Sigaud philippe.sigaud at gmail.com
Fri Jul 30 13:24:38 PDT 2010


On Fri, Jul 30, 2010 at 20:20, Andrej Mitrovic
<andrej.mitrovich at gmail.com>wrote:

> Hey, just tried out your code.
>
> In the first one there's a little typo (used 'i' instead of index), but
> that's ok. ;p
>

Ah yes, that was typed over the course of 2-3 hours, with my children
jumping around :)


>
> It seems like I don't need to use the to!() template. This code seems to
> work fine:
>
>
> void gun(T)(T value) {
>     foreach(Type; value.expand)
>         write(Type, " ");
>
>     writeln();
> }
>
>
If you just want to print them, yes. In that case btw, do not use 'Type' as
a name, as you're directly iterating on the values. Sorry, my mistake: I'm
so used to iterate on typetuples, where the iteration is done on a
index-Type pair that I typed that without thinking.



> But regardless, at least I now know how to traverse through my own tuples,
> so thanks for that one.
>

As I said, value.expand is an instantiated typetuple, to it's iterable,
indexed with [i], can be sliced with [i..j], it knows its length, etc. Very
powerful stuff, very easy to use. None of the gobbledygook from C++.

Note that .field and .expand are aliases, they are the same access to the
tuple's fields, under a different name.


>
> In your second example:
>
>
> 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);
>
> I get an error way deep in the library itself (this could call for a better
> error message, methinks):
> C:\DMD\dmd2\windows\bin\..\..\src\phobos\std\typecons.d(320): Error: tuple
> index 2 exceeds 2
>
> I've reversed the order, put types before identifiers and this works fine
> then:
>
> Tuple!(int, "index", double, "sum") t = tuple(1, 3.14);
>

Sorry also, I should have checked typecons docs.



>
> I'm not sure what you mean by "t has .first and .second as members", I
> can't seem to access those. Maybe you meant t.index and t.sum as members?
>


Hem, as I said, I typed the mail over a few hours, the first version of the
tuple was Tuple!((int, "first", double, "second"), but I found first and
second a bit bland, so I changed that to .index and .sum.

Man, I made a mess of my explanation. I'm glad you still understood
something :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100730/7160c2ef/attachment.html>


More information about the Digitalmars-d mailing list