To interface or not to interface

Steven Schveighoffer schveiguy at yahoo.com
Tue May 25 06:13:21 PDT 2010


On Tue, 25 May 2010 09:03:34 -0400, Jason House  
<jason.james.house at gmail.com> wrote:

> Walter Bright Wrote:
>
>> Jason House wrote:
>> > 7. Compiler-assisted verification.
>>
>> For interfaces, the compile time checking is limited to verifying that  
>> functions
>> with the right signature are supplied. Templates can go considerably  
>> beyond that
>> with the constraint checking.
>
> constraints are more powerful, but they have downsides:
>  • If a class is incorrectly defined, failure to use a type without a  
> constraint check leads to errors in the code using it instead of the  
> class definition. Usage isn't always guaranteed to be correct either, so  
> the developer must spend extra time diagnosing the real error.
> • If a class is incorrectly, initial usage without a constraint may  
> completely miss the error. Easy examples would be a typo propogated with  
> copy/paste, or neglecting to use save.
> • If a class is incorrectly defined and usage uses a constraint, the  
> developer will simply get an error that there is no matching call.
> • If a constraint is incorrectly defined and usage uses the constraint,  
> the developer will simply get an error that there is no matching call.
>
> None of these scenarios are particularly helpful for a developer  
> creating/expanding a family of objects.

These all boil down to the fact that you must declare an interface up  
front, whereas constraints are not required.

You can get to about the same level as an interface by using static  
asserts, but this is optional (it probably should be a "best practice"  
though somewhere).

-Steve


More information about the Digitalmars-d mailing list