Interface/abstract constructors

Steven Schveighoffer schveiguy at yahoo.com
Mon May 16 12:49:58 PDT 2011


On Mon, 16 May 2011 15:32:43 -0400, useo <unknown at unknown.com> wrote:

> Hey guys,
>
> is there any chance to create an abstract constructor like:
>
> abstract class ABC {
>
>    abstract this();
>
> }
>
> DMD always says "...this non-virtual functions cannot be abstract" -
> when I use an interface like:
>
> interface ABC {
>
>    this();
>
> }
>
> I get a similar error: "...constructors, destructors, postblits,
> invariants, unittests, new and delete functions are not allowed in
> interface ABC"
>
> Is there any solution or is it possible to create such inheritances
> in DMD?

I think what you are trying to do is say, "if a class implements interface  
ABC, it must have a default constructor".  Such a requirement is faulty.   
The point of an interface is to able to pass a portion of a class'  
functionality to a function during runtime.  However, the instance must  
*already exist*.  It makes no sense to posit requirements on the  
constructor.

What you want is a compile-time requirement using a template constraint.   
You may think "damn, but I don't want to make my function a template", I'd  
say see previous point ;)

-Steve


More information about the Digitalmars-d-learn mailing list