Advertisement
chadjoan

Constructor IFTI for righteous fakers

May 1st, 2021 (edited)
1,319
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. auto Bar(IntT)(IntT val)
  2. {
  3.     Bar!(IntT, IntT[]) bar;
  4.     bar.payload = val;
  5.     pragma(msg, "function IntT.stringof == "~IntT.stringof);
  6.     return bar;
  7. }
  8.  
  9. struct Bar(IntT, IntArr = IntT[])
  10. {
  11.     IntT payload;
  12.     pragma(msg, "struct IntT.stringof  == " ~ IntT.stringof);
  13. }
  14.  
  15. void main()
  16. {
  17.     import std.stdio;
  18.     auto bar1 = Bar(42);  // IFTI for Foo constructor!
  19.     auto bar2 = Bar("s"); // And for any type, too!
  20.     auto bar3 = Bar('c');
  21.     writefln("%d", bar1.payload); // 42
  22.     writefln("%s", bar2.payload); // s
  23.     writefln("%c", bar3.payload); // c
  24. }
  25.  
Advertisement
RAW Paste Data Copied
Advertisement