On Wednesday, 9 September 2015 at 23:44:14 UTC, Idan Arye wrote:
How about using a mixin template(http://dlang.org/template-mixin.html)?


Thanks, it's a good solution. My only reservation is I would prefer to find a way to directly invoke a symbol in std.* as otherwise different frameworks might invent their own conventions(well they might do that anyway). After a nights sleep, I actually found such a solution!

This opens the door to a new class of meta-programming; introspect the caller! :)

import std.typecons;

class Awesome1
{
private:
  int val;
this(string caller = __MODULE__)(int val) if(caller == "std.conv") // Use scoped!Awesome
  {
        this.val = val;
  }
}

class Awesome2
{
private:
  int val;
  this(string caller = __MODULE__)(int val)
  {
static assert(caller == "std.conv", "Use scoped!Awesome(...)!");

        this.val = val;
  }
}

void main()
{
  static assert(__traits(compiles, scoped!Awesome1(1)));
  static assert(__traits(compiles, scoped!Awesome2(1)));
  static assert(!__traits(compiles, new Awesome1(1)));
  static assert(!__traits(compiles, new Awesome2(1)));
}



Reply via email to