New syntax proposal for template type parameter contraints

Kagamin via Digitalmars-d digitalmars-d at puremagic.com
Wed May 21 23:38:35 PDT 2014


On Friday, 16 May 2014 at 20:31:40 UTC, Phil Lavoie wrote:
> The idea is to eventually be able to do something like this:
>
> constraint InputRange(Elt) {
>   Elt front();
>   void popFront();
>   bool empty();
> }

I have a similar idea, but I think of it as a type inference 
mechanism:

@inference
template InputRange(E=Any, S=void) {
  static if (is(S.front))
   static if (is(E==Any)||is(E==ElementType!S)) alias InputRange = 
S;
   else alias InputRange = Mismatch!"element type mismatch";
  else alias InputRange = Mismatch!"need `front` member";
}

Here E is optional parameter, which can be used to constrain 
range element type, or may be omitted to accept any element type. 
S is type of right hand expression. As a bonus, this template can 
substitute type.


More information about the Digitalmars-d mailing list