In D, how do I specify a variation pattern constraint for the entire tuple?

I am writing 2 function overloads, both of which have variable template compilation time parameters. It is necessary to take characters as patterns, other lines. I want to limit the template instance to these two cases. The best I came up with was the following:

bool func(SYMBOLS...)() if(!is(typeof(SYMBOLS[0]) == string)) { } 

and

 bool func(STRINGS...)() if(is(typeof(STRINGS[0]) == string)) { } 

Obviously, this only checks the first parameter of the template, and while it works, given the code that I have written so far, I would like to say “only for all lines” and “only for not all lines”. Is there any way?

+4
source share
2 answers

This works (from http://forum.dlang.org/thread/ qyosftfkxadeypzvtvpk@forum.dlang.org with the help of Andrei Mitrovich):

 import std.traits; import std.typetuple; void runTests(SYMBOLS...)() if(!anySatisfy!(isSomeString, typeof(SYMBOLS))) { } void runTests(STRINGS...)() if(allSatisfy!(isSomeString, typeof(STRINGS))) { } 
+6
source

It took me a while to figure this out, but here is a potential solution to your problem:

 module main; import std.stdio; int main(string[] argv) { bool test1PASS = func(1,2,3,4,5.5, true); //bool test2CTE = func(1,2,3,4,5, true, "CRAP"); bool test3PASS = func("CRAP", "3", "true"); //bool test4CTE = func("CRAP", "3", "true", 42, true, 6.5); return 0; } bool func(NOSTRINGS...)(NOSTRINGS x) if ({foreach(t; NOSTRINGS) if (is(t == string)) return false; return true; }()) { // code here ... return true; } bool func(ONLYSTRINGS...)(ONLYSTRINGS x) if ({foreach(t; ONLYSTRINGS) if (!is(t == string)) return false; return true; }()) { // code here ... return true; } 
+6
source

Source: https://habr.com/ru/post/1492089/


All Articles