Advertisement
Guest User

d test

a guest
Jul 17th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.95 KB | None | 0 0
  1. /*
  2. bool foo(T)(const(char)[] id, T arg)
  3. if (is(T : const(char)[]) || is(T : const(char[])[]))
  4. {
  5.         static if (is(T : const(char)[])) {
  6.                 return id == arg;
  7.         } else {
  8.                 foreach (s; arg) if (s == id) return true;
  9.         }
  10.         return false;
  11. }
  12. */
  13.  
  14. bool foo(const(char)[] id, const(char[])[]  arg ...)
  15. {
  16.     foreach (s; arg) if (s == id) return true;
  17.     return false;
  18. }
  19.  
  20. void main()
  21. {
  22.     string id = "id";
  23.    
  24.     char[] a = "aaaa".dup;
  25.     const(char)[] b = "bbbb";
  26.     immutable(char)[] c = "cccc";
  27.     foo(id, a);
  28.     foo(id, b);
  29.     foo(id, c);
  30.     char[][] aa = ["aaaa".dup];
  31.     const(char)[][] bb = ["bbbb"];
  32.     immutable(char)[][] cc = ["cccc"];
  33.     foo(id, aa);
  34.     foo(id, bb);
  35.     foo(id, cc);
  36.    
  37.     // these works with the typesafe variadic function only
  38.     foo("id", "aaaa", "bbbb");
  39.     foo("id", "aaaa", "bbbbb", "cccc");
  40.    
  41.     // these should always fail
  42.     dstring w = "wwww";
  43.     dstring[] ww = ["wwww"];
  44.     foo(id, w);
  45.     foo(id, ww);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement