Advertisement
chadjoan

Constuctor IFTI, and it's JANK

May 1st, 2021
3,459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.74 KB | None | 0 0
  1. auto Foo(Char)(Char[] text)
  2.     if ( !is(Char T : T[]) ) // Basically just ensure that `Char` isn't a string or array.
  3. {
  4.     import std.stdio;
  5.     Foo!(Char[]) foo;
  6.     foo.payload = text;
  7.     pragma(msg, "function Char.stringof == "~Char.stringof);
  8.     return foo;
  9. }
  10.  
  11. struct Foo(StrT)
  12.     if ( is(StrT CharT : CharT[]) ) // Ensure that `StrT` is a string|array; declare `CharT` as it's element type.
  13. {
  14.     StrT payload;
  15.     pragma(msg, "struct StrT.stringof  == " ~ StrT.stringof);
  16.  
  17.     /// Bonus jank: These fail to compile because `CharT` isn't declared /in this scope/.
  18.     // CharT ch;
  19.     // pragma(msg, "struct CharT.stringof == " ~ CharT.stringof);
  20. }
  21.  
  22. void main()
  23. {
  24.     import std.stdio;
  25.     auto foo = Foo("x"); // IFTI for Foo constructor!
  26.     writefln(foo.payload);
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement