Using return type of a predicate function as a template

Edwin van Leeuwen via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 16 01:04:06 PDT 2014


I am trying to implement a groupBy function that groups by the 
return type of a predicate. Currently I have to define the 
returntype of the predicate for it to compile. Is there a way to 
get the return type at compile time and use it.

The code:
V[K] groupBy( alias func, K, V )( V values )
{
   V[K] grouped;
   foreach ( value ; values ) {
     grouped[func( value )] ~= value;
   }
   return grouped;
}

unittest {
   struct Test {
     string a;
     double b;
   }

   auto values = [Test( "a", 1 ), Test( "a", 2 ), Test( "b", 3 )];
   auto grouped = values.groupBy!( (a) => a.a, string );
   assert( grouped["a"].length == 2 );
   assert( grouped["a"][1].b == 2 );
   assert( grouped["b"].length == 1 );
   assert( grouped["b"][0].b == 3 );
}

So the above works, but I need to call it with:
values.groupBy!( (a) => a.a, string );
Ideally I would call it instead with:
values.groupBy!( (a) => a.a )
and it would infer that the template K needs to be a string, 
since that is the return type of (a) => a.a.

Cheers,

Edwin


More information about the Digitalmars-d-learn mailing list