why can't structs implement interfaces?

Bill Baxter wbaxter at gmail.com
Tue Nov 24 15:18:03 PST 2009


On Tue, Nov 24, 2009 at 2:36 PM, bearophile <bearophileHUGS at lycos.com> wrote:
> Moritz Warning:
>
>> If you only what a contract that certain functions are implemented,
>> then it just need to be implemented in the compiler frontend.
>
> In the meantime this can be done with a template mixin, where the template statically asserts the presence of the functions/fields you want.

I really think some kind of static interface (concept) support is
going to be necessary eventually.

In terms of functionality, static asserts and "isInterfaceSupported()"
methods are OK.  But the usability is not great.
In particular there's not a good way for the compiler to give good
error messages about why a concept is not satisfied by a particular
type.  I tried to come up with a way to do that given access to
compiler error messages, but the result looks rather like attempts to
do runtime inheritance in C.
The good thing is that since most of the machinery is there, the
actual compiler changes required would mostly be just rewrites of new
syntax in terms of existing functionality.

--bb


More information about the Digitalmars-d-learn mailing list