Thread overview |
---|
April 28, 2008 [Issue 2051] New: interfaces should allow private methods | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=2051 Summary: interfaces should allow private methods Product: D Version: unspecified Platform: PC OS/Version: Linux Status: NEW Severity: normal Priority: P2 Component: DMD AssignedTo: bugzilla@digitalmars.com ReportedBy: andrei@metalanguage.com Consider: interface Foo { private void bar(); } class Bar : Foo { } void main() { Foo bar = new Bar; bar.bar; } This code has a link-time error. Removing "private" yields (correctly) a compile-time error. So we have a bug already :o). Adding a function void bar() {} to Bar keeps the link-time error in vigor, whether or not the function is private. Now, if we want D to allow Sutter's NVI idiom (http://www.gotw.ca/publications/mill18.htm) easily, it would need to enact the following rules: * A private method in an interface is part of that interface's table of signatures. * A class implementing a private method in an interface cannot graduate its visibility (e.g. protected or public). With these rules in place, D would support NVI very naturally. I think that's a powerful paradigm that would lead to very expressive and robust interfaces. -- |
April 28, 2008 Re: [Issue 2051] New: interfaces should allow private methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | d-bugmail@puremagic.com wrote:
> Consider:
>
> interface Foo
> {
> private void bar();
> }
>
> class Bar : Foo
> {
> }
>
> void main()
> {
> Foo bar = new Bar;
> bar.bar;
> }
>
> This code has a link-time error. Removing "private" yields (correctly) a
> compile-time error. So we have a bug already :o).
The error occurs because there's no implementation anywhere, so the vtable can't be completed. I'm assuming you're suggesting that this should be turned into a runtime error if no implementation is present anywhere, but this would be so much harder to update stuff once an interface is updated. I can't see any advantages of making this error runtime.
|
April 28, 2008 [Issue 2051] interfaces should allow private methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=2051 ------- Comment #2 from kamm-removethis@incasoftware.de 2008-04-28 07:16 ------- By the current D spec private functions are never virtual. I'm pretty sure that's why having a private function in an interface won't work. -- |
April 28, 2008 [Issue 2051] interfaces should allow private methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=2051 ------- Comment #3 from andrei@metalanguage.com 2008-04-28 11:15 ------- (In reply to comment #1) > d-bugmail@puremagic.com wrote: > > Consider: > > > > interface Foo > > { > > private void bar(); > > } > > > > class Bar : Foo > > { > > } > > > > void main() > > { > > Foo bar = new Bar; > > bar.bar; > > } > > > > This code has a link-time error. Removing "private" yields (correctly) a > > compile-time error. So we have a bug already :o). > > The error occurs because there's no implementation anywhere, so the vtable can't be completed. I'm assuming you're suggesting that this should be turned into a runtime error if no implementation is present anywhere, but this would be so much harder to update stuff once an interface is updated. I can't see any advantages of making this error runtime. The rest of my post clarifies my intent, which is not to make the error runtime. -- |
April 28, 2008 [Issue 2051] interfaces should allow private methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=2051 ------- Comment #4 from andrei@metalanguage.com 2008-04-28 11:27 ------- (In reply to comment #2) > By the current D spec private functions are never virtual. I'm pretty sure that's why having a private function in an interface won't work. That explains part of the odd behavior - the compiler has a conflict of interest of sorts. Now that I think of it, probably protected functions in an interface should be enough. Consider: interface Foo { protected void bar(); } class Bar : Foo { void bar() {} } void main() { Foo bar = new Bar; bar.bar; } This goes through, and is probably enough to make NVI work. There is the minor annoyance that the class Bar can graduate visibility from protected to public, but if it wants to, no language rule can stop it (e.g. Bar could expose the method under a different name). I will leave the bug opened because at a minimum the linker error should be really a compile-time error. Alternatively, private functions in interfaces could be allowed because they are technically final. Thanks all for your comments. -- |
January 30, 2012 [Issue 2051] interfaces should allow private methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=2051 yebblies <yebblies@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |mathias.baumann@sociomantic | |.com --- Comment #5 from yebblies <yebblies@gmail.com> 2012-01-30 15:10:49 EST --- *** Issue 6170 has been marked as a duplicate of this issue. *** -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
January 30, 2012 [Issue 2051] interfaces should allow private methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=2051 yebblies <yebblies@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED CC| |yebblies@gmail.com Platform|x86 |All Version|1.00 |D1 & D2 Resolution| |INVALID OS/Version|Linux |All --- Comment #6 from yebblies <yebblies@gmail.com> 2012-01-30 15:13:29 EST --- This is intentional, as by D's compilation model the body of the function may be provided in another object file. eg. When using .di files. The original bug has been fixed by allowing final methods in interfaces. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation