@disable("reason")

Simen Kjærås simen.kjaras at gmail.com
Wed Jan 8 11:53:56 UTC 2020


On Wednesday, 8 January 2020 at 08:26:51 UTC, user1234 wrote:
> 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`

However, it will print that message even if the constructor is 
never called. If you make the constructor a template instead, you 
will only get the message when someone attempts to use the 
default constructor:

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

void main()
{
     new Example();
}

Sadly, this does not work for structs, as they don't really have 
a default constructor, as Jonathan pointed out.

--
   Simen


More information about the Digitalmars-d-learn mailing list