www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to pass voldemort types to functions

reply "kerdemdemir" <kerdemdemir hotmail.com> writes:
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?
Jun 12 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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. :)
Jun 12 2015
parent reply "kerdemdemir" <kerdemdemir hotmail.com> writes:
 void foo(R)(R range)
     if (isInstanceOf!(Tuple, ElementType!R))    // <-- optional
 {
 
Ali thanks a lot. I don't believe I didn't simply try your way. It works. I am also happy to learn optional static if . Your examples are really useful for me. Next time I will share whole code. Thanks a lot.
Jun 12 2015
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/12/2015 03:46 PM, kerdemdemir wrote:

 void foo(R)(R range)
     if (isInstanceOf!(Tuple, ElementType!R))    // <-- optional
 {
 I am also happy to learn optional static if .
Actually, it is not 'static if' but a template constraint. It is optional because everything will work without it. If you call it with a range where elements are not Tuples and the function really requires that they are (e.g. the function does not compile if it is not Tuple), then the location of the error message will be different: a) With template constraint the caller is in error b) Without template constraint the function is in error Ali
Jun 12 2015