Now that's a DIP that could use some love
    Adam D. Ruppe 
    destructionator at gmail.com
       
    Mon Sep 14 00:12:34 UTC 2020
    
    
  
On Monday, 14 September 2020 at 00:00:15 UTC, Adam D. Ruppe wrote:
> While that would be possible, I believe a better DIP would be 
> to let the compiler recognize the pattern and work it 
> automatically for us.
Here's a slight refinement: it does the cast to bool already. So 
we can return a struct with opCast. But how to get the message 
out?
It could be a magic type that the compiler recognizes...
Or it could just be a pattern. Suppose template constraints as 
well as static assert (heck maybe even normal assert) just looked 
for a method, maybe __compileErrors. If present, it processes the 
return value as supplemental errors.
Now the library defines
struct MyChecks {
     bool opBool(T:bool) { return errors.length == 0; }
     string[] __compileErrors;
}
MyChecks isInputRange(T)() {
      MyChecks errors;
      if(!hasMember!(T, "front")
         errors.__compileErrors ~= "missing front";
      if(!hasMember!(T, "popFront")
         errors.__compileErrors ~= "missing popFront";
      if(!hasMember!(T, "empty")
         errors.__compileErrors ~= "missing empty";
      return errors;
}
Then user code does:
static assert(isInputRange!MyThing); // automatically shows the 
errors if it fails (unless the user specifies something else)
void foo(T)() if(isInputRange!T) {} // shows the errors if it 
fails  in addition to what it has now
Destroy.
    
    
More information about the Digitalmars-d
mailing list