private method in interface

Steven Schveighoffer schveiguy at yahoo.com
Thu Jun 2 12:19:30 PDT 2011


On Thu, 02 Jun 2011 15:01:31 -0400, Michael Shulman  
<viritrilbia at gmail.com> wrote:

> On Thu, Jun 2, 2011 at 10:31 AM, Steven Schveighoffer
> <schveiguy at yahoo.com> wrote:
>> Private methods are non-virtual, so I'm pretty sure they are not  
>> supposed to
>> be allowed in an interface.
>>
>> But I suppose private could also mean private final, in which case you  
>> have
>> to provide an implementation for foo in the interface.
>
> In section 6.9.1 of "The D Programming Language" about Non-Virtual
> Interfaces, there is an example (p214) of an interface which defines
> two private methods without implementation.  But now that you point it
> out, that code also fails the linker for me.  Is that book out of sync  
> with
> the implementation?

I'm not sure, I don't have a copy of the book, so I'm not sure of the  
context.  I remember reading posts claiming that the book intends for  
private methods to be virtual, but I can't be sure.

All I know is, the official position of D is that the book overrules the  
current spec/implementation.  So if it seems the book is not agreeing with  
the current spec, it's likely a bug in the spec/compiler.  If you believe  
this to be the case, please file a bug on bugzilla   
http://d.puremagic.com/issues/enter_bug.cgi  Put [TDPL] at the beginning  
of the summary.

>
>> This would be in line with the error message.
>
> It is true that if I replace "private" with "final" I get the same
> error message.  That is also puzzling to me; I would expect a final
> method in an interface without an implementation to be a *compiler*
> error, since there is no way anyone else can implement it.  Is there?

In D, you do not need to provide an implementation when declaring a  
function.  For example, if you want to hide the details of a function, you  
can just declare it in a public file, but actually build your library with  
a private file that contains the implementation.  This way, the end-user  
cannot see the source.  .di files are supposed to be for this, but I think  
in practice, .di and .d files are treated the same by the compiler.

You can see a great example of this in object.di included with the  
compiler (src/druntime/import/object.di).

-Steve


More information about the Digitalmars-d-learn mailing list