Thread overview | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 02, 2011 private method in interface | ||||
---|---|---|---|---|
| ||||
Hi, The following code fails the linker, complaining about an undefined reference to 'foo': interface A { private int foo(); } class B { int bar(A x) { return x.foo(); } } void main() { } But if I remove the 'private' qualifier on 'foo', then it succeeds. Shouldn't B.bar() be able to access the private A.foo(), since they are in the same package? If I move the definition of A into another package, then the *compiler* fails with "interface member not accessible", as I would expect. Thanks! Mike |
June 02, 2011 Re: private method in interface | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael Shulman | On Thu, 02 Jun 2011 13:10:14 -0400, Michael Shulman <viritrilbia@gmail.com> wrote:
> Hi,
>
> The following code fails the linker, complaining about an undefined
> reference to 'foo':
>
>
> interface A {
> private int foo();
> }
>
> class B {
> int bar(A x) { return x.foo(); }
> }
>
> void main() { }
>
>
> But if I remove the 'private' qualifier on 'foo', then it succeeds.
> Shouldn't B.bar() be able to access the private A.foo(), since they
> are in the same package? If I move the definition of A into another
> package, then the *compiler* fails with "interface member not
> accessible", as I would expect.
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.
This would be in line with the error message.
-Steve
|
June 02, 2011 Re: private method in interface | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Thu, Jun 2, 2011 at 10:31 AM, Steven Schveighoffer <schveiguy@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? > 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? Mike |
June 02, 2011 Re: private method in interface | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael Shulman | On Thu, 02 Jun 2011 15:01:31 -0400, Michael Shulman <viritrilbia@gmail.com> wrote: > On Thu, Jun 2, 2011 at 10:31 AM, Steven Schveighoffer > <schveiguy@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 |
June 02, 2011 Re: private method in interface | ||||
---|---|---|---|---|
| ||||
On 2011-06-02 12:01, Michael Shulman wrote: > On Thu, Jun 2, 2011 at 10:31 AM, Steven Schveighoffer > > <schveiguy@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? > > > 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? http://d.puremagic.com/issues/show_bug.cgi?id=4542 http://d.puremagic.com/issues/show_bug.cgi?id=2051 - Jonathan M Davis |
June 02, 2011 Re: private method in interface | ||||
---|---|---|---|---|
| ||||
On Thu, Jun 2, 2011 at 12:20 PM, Jonathan M Davis <jmdavisProg@gmx.com> wrote: > http://d.puremagic.com/issues/show_bug.cgi?id=4542 http://d.puremagic.com/issues/show_bug.cgi?id=2051 Thank you! I think this answers my question completely; I just need to change "private" to "protected". Is there a place on the web recording "errata" of this sort for TDPL? Here is a related question which puzzles me, from reading http://d-programming-language.org/function.html#virtual-functions What is the difference between "private" and "final private"? Mike |
June 02, 2011 Re: private method in interface | ||||
---|---|---|---|---|
| ||||
On 2011-06-02 12:59, Michael Shulman wrote: > On Thu, Jun 2, 2011 at 12:20 PM, Jonathan M Davis <jmdavisProg@gmx.com> wrote: > > http://d.puremagic.com/issues/show_bug.cgi?id=4542 http://d.puremagic.com/issues/show_bug.cgi?id=2051 > > Thank you! I think this answers my question completely; I just need to change "private" to "protected". Is there a place on the web recording "errata" of this sort for TDPL? http://erdani.com/tdpl/errata/index.php?title=Main_Page > Here is a related question which puzzles me, from reading http://d-programming-language.org/function.html#virtual-functions What is the difference between "private" and "final private"? At present, there is no difference between a member function which is private and one which is final private. All private functions are non-virtual and are not overridable. So, final does nothing. If/When dmd is updated to match TDPL and make private functions virtual and overridable, then private functions _will_ be virtual and overridable, and final will be required in order to make them non-overridable again (and assuming that the private function in question does not override a private funtion from a base class, the compiler should be able to optimize it so that it's non-virtual just like private currently is). But for now, putting final on a private function does nothing. - Jonathan M Davis |
June 03, 2011 Re: private method in interface | ||||
---|---|---|---|---|
| ||||
Thanks!
On Thu, Jun 2, 2011 at 2:36 PM, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
> On 2011-06-02 12:59, Michael Shulman wrote:
>> On Thu, Jun 2, 2011 at 12:20 PM, Jonathan M Davis <jmdavisProg@gmx.com>
> wrote:
>> > http://d.puremagic.com/issues/show_bug.cgi?id=4542 http://d.puremagic.com/issues/show_bug.cgi?id=2051
>>
>> Thank you! I think this answers my question completely; I just need to change "private" to "protected". Is there a place on the web recording "errata" of this sort for TDPL?
>
> http://erdani.com/tdpl/errata/index.php?title=Main_Page
>
>> Here is a related question which puzzles me, from reading http://d-programming-language.org/function.html#virtual-functions What is the difference between "private" and "final private"?
>
> At present, there is no difference between a member function which is private and one which is final private. All private functions are non-virtual and are not overridable. So, final does nothing. If/When dmd is updated to match TDPL and make private functions virtual and overridable, then private functions _will_ be virtual and overridable, and final will be required in order to make them non-overridable again (and assuming that the private function in question does not override a private funtion from a base class, the compiler should be able to optimize it so that it's non-virtual just like private currently is).
>
> But for now, putting final on a private function does nothing.
>
> - Jonathan M Davis
>
|
June 03, 2011 Re: private method in interface | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | Jonathan M Davis Wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=4542 http://d.puremagic.com/issues/show_bug.cgi?id=2051
Nothing prevents compiler from considering class private methods as final and allow implement interface private methods (possibly with requirement for `override` keyword).
|
June 03, 2011 Re: private method in interface | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On 2011-06-02 23:53, Kagamin wrote:
> Jonathan M Davis Wrote:
> > http://d.puremagic.com/issues/show_bug.cgi?id=4542 http://d.puremagic.com/issues/show_bug.cgi?id=2051
>
> Nothing prevents compiler from considering class private methods as final and allow implement interface private methods (possibly with requirement for `override` keyword).
It would be possible but inconsistent. It would also likely be more complicated to implement than simply making private functions virtual and overridable. I don't really like the idea of private functions being virtual by default though, so maybe your suggestion would be a good one.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation