Advertisement
Guest User

Untitled

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