@disable("reason")

user1234 user1234 at 1234.de
Wed Jan 8 08:26:51 UTC 2020


On Wednesday, 8 January 2020 at 00:23:48 UTC, Marcel wrote:
> Hello!
> I'm writing a library where under certain conditions i need all 
> the default constructors to be disabled. I would like to tell 
> the user why they can't instantiate the struct.
> Is there a way to do that?

class Example
{
     @disable this() { pragma(msg, "not allowed..."); }
}

void main()
{
     new Example();
}

outputs:

> not allowed...
> /tmp/temp_7F8C65489550.d(12,5): Error: constructor 
> `runnable.Example.this` cannot be used because it is annotated 
> with `@disable`

Because pragma(msg) are evaluated when encountered you can use 
abuse them.
By specification they're not allowed to modify the meaning of the 
program.


More information about the Digitalmars-d-learn mailing list