December 05, 2005 Re: Question about 'final' keyword. | ||||
---|---|---|---|---|
| ||||
Posted in reply to AjO | In article <dn2aqq$287q$1@digitaldaemon.com>, AjO says... > >In article <dn29eo$26d2$1@digitaldaemon.com>, Kramer says... >> >>Also, 'final' I'm guessing would remove the need for methods to be virtual, thus potentially speeding up those methods, no? >> >>-Kramer > >Well, it says in the D spec that there is no virtual table. Or that's what it says at: http://www.digitalmars.com/d/module.html > That page is for modules, not classes. For classes, all non-private, non-package and non-final methods are vtabled. I think the docs need to be expanded on what attributes are supposed to do for class definitions as opposed to class members - "private class" for example is allowed by the compiler, but apparently doesn't do anything; you can still new a private class in another module for example. 'final class' however does seem to effect just the member functions, marking them final as you mentioned earlier. |
December 06, 2005 Re: Question about 'final' keyword. | ||||
---|---|---|---|---|
| ||||
Posted in reply to AjO | On Mon, 5 Dec 2005 20:11:15 +0000 (UTC), AjO wrote: > Hello everyone! > > I was today looking into the similarities/differences of Java and D. I experimented with the 'final' keyword. In Java, when used with class declaration, guarantees that one cannot subclass from a 'final' class. Now, I tried the similar approach in D, and to my surprise, noticed that with the code provided below, only the methods become final. This means that I can subclass a 'final' class in D with no problem. All I can't do is override the methods in that 'final' class. Is this desired behaviour, or is the something wrong with my snippet? No, there is no error in your code. 'final' and 'private' apply to functions and not classes. There is no such thing as a final class or a private class in D. There are classes that can contain final functions and/or private functions. -- Derek (skype: derek.j.parnell) Melbourne, Australia "A learning experience is one of those things that says, 'You know that thing you just did? Don't do that.'" - D.N. Adams 6/12/2005 11:54:18 AM |
December 06, 2005 Re: Question about 'final' keyword. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | "Derek Parnell" <derek@psych.ward> wrote in message news:1w14cf2n0yaen$.1fya63bj3ql24$.dlg@40tude.net... > There is no such thing as a final class or a > private class in D. I really wish there were private classes.. |
December 06, 2005 Re: Question about 'final' keyword. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | There are. You make the ctor private. "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:dn2o4p$2loa$1@digitaldaemon.com... > "Derek Parnell" <derek@psych.ward> wrote in message news:1w14cf2n0yaen$.1fya63bj3ql24$.dlg@40tude.net... >> There is no such thing as a final class or a >> private class in D. > > I really wish there were private classes.. > |
December 06, 2005 Re: Question about 'final' keyword. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | Not much of a private if you ask me. IMO private means invisibility and not "uninstantiability". So the symbol must NOT be viewable by the outside user of the module. Tom PS: Not my intention to restart an old discussion, but private (at module level) should mean invisible. In article <dn2o7c$2lu2$1@digitaldaemon.com>, Kris says... > >There are. You make the ctor private. > >"Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:dn2o4p$2loa$1@digitaldaemon.com... >> "Derek Parnell" <derek@psych.ward> wrote in message news:1w14cf2n0yaen$.1fya63bj3ql24$.dlg@40tude.net... >>> There is no such thing as a final class or a >>> private class in D. >> >> I really wish there were private classes.. |
December 06, 2005 Re: Question about 'final' keyword. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | Doesn't work. Classes with private ctors can still be constructed. I think ctors are always implicitly public in D.
Kris wrote:
> There are. You make the ctor private.
>
> "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:dn2o4p$2loa$1@digitaldaemon.com...
>> "Derek Parnell" <derek@psych.ward> wrote in message news:1w14cf2n0yaen$.1fya63bj3ql24$.dlg@40tude.net...
>>> There is no such thing as a final class or a
>>> private class in D.
>> I really wish there were private classes..
>>
>
>
|
December 06, 2005 Re: Question about 'final' keyword. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | That's a bug, then. I've recently noticed private doesn't make a darned bit of difference, regardless of what one applies it to. This was not always the case ~ private used to actually do "something" :-) "Sean Kelly" <sean@f4.ca> wrote in message news:dn33tc$2uqa$2@digitaldaemon.com... > Doesn't work. Classes with private ctors can still be constructed. I think ctors are always implicitly public in D. > > Kris wrote: >> There are. You make the ctor private. >> >> "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:dn2o4p$2loa$1@digitaldaemon.com... >>> "Derek Parnell" <derek@psych.ward> wrote in message news:1w14cf2n0yaen$.1fya63bj3ql24$.dlg@40tude.net... >>>> There is no such thing as a final class or a >>>> private class in D. >>> I really wish there were private classes.. >>> >> |
December 06, 2005 Re: Question about 'final' keyword ~ private not working? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | Jumped the gun a bit: ================= module B; class B { private this() {} private void bar() {} } struct X { private static final void error() {} } ================= module A; import B; void main() { X x; X.error(); // bug! can see X.error x.error(); // cannot see x.error auto b = new B; // cannot see ctor b.bar(); // cannot see method } ================== There's just the one case above that fails ~ I've being writing a lot of structs lately. I guess that's a bug? - Kris "Kris" <fu@bar.com> wrote in message news:dn359j$30lp$1@digitaldaemon.com... > That's a bug, then. > > I've recently noticed private doesn't make a darned bit of difference, regardless of what one applies it to. This was not always the case ~ private used to actually do "something" :-) > > > "Sean Kelly" <sean@f4.ca> wrote in message news:dn33tc$2uqa$2@digitaldaemon.com... >> Doesn't work. Classes with private ctors can still be constructed. I think ctors are always implicitly public in D. >> >> Kris wrote: >>> There are. You make the ctor private. >>> >>> "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:dn2o4p$2loa$1@digitaldaemon.com... >>>> "Derek Parnell" <derek@psych.ward> wrote in message news:1w14cf2n0yaen$.1fya63bj3ql24$.dlg@40tude.net... >>>>> There is no such thing as a final class or a >>>>> private class in D. >>>> I really wish there were private classes.. >>>> >>> > |
December 06, 2005 Re: Question about 'final' keyword ~ private not working? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | Kris wrote: > Jumped the gun a bit: > > ================= > module B; > > class B > { > private this() {} > private void bar() {} > } > > struct X > { > private static final void error() {} > } > > ================= > > module A; > > import B; > > void main() > { > X x; > > X.error(); // bug! can see X.error > x.error(); // cannot see x.error > auto b = new B; // cannot see ctor Nice. This has changed since I last tested it then. > b.bar(); // cannot see method > } > > ================== > > There's just the one case above that fails ~ I've being writing a lot of structs lately. I guess that's a bug? Looks like it. And as a side-note, private template members are not visible at module scope. This one drives me a bit crazy from time to time :-) Sean |
December 06, 2005 Re: Question about 'final' keyword ~ private not working? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kris | On Mon, 5 Dec 2005 21:13:23 -0800, Kris wrote: > > ================= > module B; > struct X > { > private static void error() {} > } > > ================= > > module A; > > import B; > > void main() > { > X x; > > X.error(); // bug! can see X.error > } I think this is related to an earlier bug in which by supplying an explicit name qualifier, it overrides the 'private' attribute. For example ... ========================== module B; private static void ps_error() {} static void s_error() {} private void p_error() {} ========================== module A; import B; void main() { B.ps_error(); // ok but shouldn't be. B.s_error(); // ok B.p_error(); // ok but shouldn't be. ps_error(); // fails as expected. s_error(); // ok p_error(); // fails as expected. } ================= And to make it worse, the compiler doesn't give us line numbers in the correct fail situation. ... C:\temp>dmd a b a.d: module A B.ps_error is private a.d: module A B.p_error is private -- Derek (skype: derek.j.parnell) Melbourne, Australia "A learning experience is one of those things that says, 'You know that thing you just did? Don't do that.'" - D.N. Adams 6/12/2005 5:34:34 PM |
Copyright © 1999-2021 by the D Language Foundation