friends with phobos, workaround?

Daniel N via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 10 02:02:30 PDT 2015


On Thursday, 10 September 2015 at 08:22:29 UTC, Daniel N wrote:
> 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)));
> }

PS 'private:' should of course be 'public:' when using this idiom.



More information about the Digitalmars-d-learn mailing list