One more simple idea for simplifying constraints

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Mon Apr 25 11:50:28 PDT 2016


Sometimes we use constraints to distinguish different approach to 
implementation, e.g. find for a string is different than find for 
forward ranges is different from find for input ranges etc.

One simple idea here (which Walter also advised a while ago) is to push 
implementation details inside the, well, implementation, e.g.:

Type functionName(TemplateArgs)(Args)
if (HighLevelConstraint)
{
   static if (LowLevelCapability1)
   {
     .. specialization 1 ...
   }
   else static if (LowLevelCapability1)
   {
     ... specialization 2 ...
   }
   else
   {
     .. conservative impl ...
   }
}

That way the user/documentation only deals with the high level 
constraint (the minimum necessary for the function to work). Inside, the 
function decides the exact approach.

It's an obvious idea now but many times in the past I didn't pursue it.


Andrei


More information about the Digitalmars-d mailing list