"if not" condition check (for data validation)

Ali Çehreli acehreli at yahoo.com
Thu Jun 18 17:57:49 UTC 2020


On 6/18/20 5:13 AM, Denis wrote:

 > Templates offer a clean syntax

Here is an earlier experiment of nested templates, which may be useful 
in this case. This is unrelated to your problem but the syntax can be 
pretty readable with templates:

// If there are template arguments, then the result is the first of
// them. If there is no argument, then the result is the argument
// of 'otherwise'.
template FirstOf(T...) {
   template otherwise(alias D) {
     static if (T.length != 0) {
       enum otherwise = T[0];

     } else {
       enum otherwise = D;
     }
   }
}

unittest {
   static assert (FirstOf!(1.5, "hello").otherwise!100 == 1.5);
   static assert (FirstOf!().otherwise!42 == 42);
}

auto foo(Args...)() {
   auto temp = FirstOf!Args.otherwise!1.5;
   // ...
   return temp + 0.5;
}

unittest {
   assert(foo!(10, int[])() == 10.5);
   assert(foo() == 2.0);
}

void main() {
}

I think you should be able to pass callables as 'alias' template 
arguments but I couldn't put much thought into it.

Ali



More information about the Digitalmars-d-learn mailing list