How to pass voldemort types to functions
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jun 12 15:36:23 PDT 2015
On 06/12/2015 03:19 PM, kerdemdemir wrote:
> Hi;
>
> I have tuples created by std.algorithm.group function.
>
> auto tupleB = stringB.group();
>
> I need to write a a function which takes tubleB and do some cool stuff.
> If I don't use a function and write all code below .group() everytihng
> works but for reusing the code I want to call a function with my tuples.
>
> I tried ;
>
> void foo(T...)(T tuple)
> void foo(Tuple!(dchar,uint) tuplle)
>
> But even couldn't compiile.
>
> Do you have any idea for passing result of std.algorithm.group() to my
> free function?
According to group()'s documentation, the elements of the range are
Tuples (not the range itself). Best thing to do is to use a template and
optionally require that the elements are instances of the Tuple template.
import std.stdio;
import std.algorithm;
import std.typecons;
import std.traits;
import std.range;
void foo(R)(R range)
if (isInstanceOf!(Tuple, ElementType!R)) // <-- optional
{
writefln("%(%s\n%)", range);
}
void main()
{
int[] arr = [ 1, 2, 2, 2, 2, 3, 4, 4, 4, 5 ];
foo(arr.group);
}
Prints
Tuple!(int, uint)(1, 1)
Tuple!(int, uint)(2, 4)
Tuple!(int, uint)(3, 1)
Tuple!(int, uint)(4, 3)
Tuple!(int, uint)(5, 1)
Ali
P.S. I know that you like being brief but I find it easier if you
provide complete code. :)
More information about the Digitalmars-d-learn
mailing list