Thread overview | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 17, 2004 Rename ctor / dtor | ||||
---|---|---|---|---|
| ||||
Hi..
<preamble>
How is D's this(...) and ~this(...) for the constructor and destructor for a class an improvement over C++ / Java's classname(...) / ~classname(...) ?
I guess renaming a class just got easier. But is this worth introducing a completely new meaning for an existing keyword, 'this'? (note: I regard changing 'this' to be the actual instance instead of a pointer to it not a 'completely new meaning')
Come to think of it, when creating a new instance of a class "new Car(...)", it's more obvious that the class' function Car(...) gets called than this(...). Also, it's impossible to store a pointer (delegate) to a class' constructor for later reinitialisation, because "&this" won't work (I don't know if this is actually possible, but it would sure eliminate Init() methods in many of my classes).
The only special thing about the constructor and destructor is that they implicitly construct/destruct the class' members and inherited classes. They are just methods.
</preamble>
<suggestion>
Would changing the names to "constructor" and "destructor" (or "ctor" and "dtor", or "construct" / "destruct") not make more sense?
</suggestion>
I guess this would lower the threshold for new programmers to use D even more. I hope I don't get only "too late to make such a rigorous change" ractions :-/ (It seems I guess I hope and I think a lot :-S)
(A Python PEP-like system would be very nice for D. All these loose suggestions that get lost in the newsgroup after some time is a real pity. Another suggestion.)
--
Lionello.
-- Get the root certificate at https://www.cacert.org/
|
December 17, 2004 Re: Rename ctor / dtor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | Lionello Lunesu wrote: > How is D's this(...) and ~this(...) for the constructor and destructor for a class an improvement over C++ / Java's classname(...) / ~classname(...) ? [...] > Would changing the names to "constructor" and "destructor" (or "ctor" and "dtor", or "construct" / "destruct") not make more sense? I think you mean constTractor and deathTractor, the new names :) Seriously, I don't think "construct" and "destruct" looks bad... Look at what PHP 5 uses, for instance: http://talks.php.net/show/php5_ca/3 "new" and "delete" are also in use, for allocator/deallocator: http://www.digitalmars.com/d/memory.html#newdelete The current name, "this", reflects to how it is being called from another constructor, which works the same as in Java : > class C > { > int j; > this() > { > ... > } > this(int i) > { > this(); > j = i; > } > } So if you changed the names, *that* call would look more obscure... ? (since destructors aren't called explicitly, their name matters less) Not that it would be *very* strange to instead call eg. "construct()". > I guess this would lower the threshold for new programmers to use D even more. I hope I don't get only "too late to make such a rigorous change" ractions :-/ (It seems I guess I hope and I think a lot :-S) "this" and "~this" are somewhat obscure names, that much is true... The only improvement over C++ / Java is not requiring the class name, at the expense of losing "instant recognition" for those used to them... For newcomers, "construct" and "destruct" are probably easier to grasp ? > (A Python PEP-like system would be very nice for D. All these loose suggestions that get lost in the newsgroup after some time is a real pity. Another suggestion.) A list of bugs would be nice too... (like Bugzilla or so) But for now, there are just: http://www.prowiki.org/wiki4d/wiki.cgi?PendingPeeves http://www.prowiki.org/wiki4d/wiki.cgi?FeatureRequestList Currently most of them are just piling up, for either Walter (DMD) or David (GDC), to take a look at when they're able... --anders |
December 17, 2004 Re: Rename ctor / dtor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | Hi, >> Would changing the names to "constructor" and "destructor" (or "ctor" and "dtor", or "construct" / "destruct") not make more sense? > > Seriously, I don't think "construct" and "destruct" looks bad... > > Look at what PHP 5 uses, for instance: http://talks.php.net/show/php5_ca/3 Ah, summing it all up: I think "construct" and "destruct" are the best options too, because... * they're actual english words (as opposed to ctor/dtor); * they actually describe what's going on; * PHP5 uses them too (be it with "__", which I'd drop); * they don't cause any conflicts (within D itself that is). > "new" and "delete" are also in use, for allocator/deallocator: http://www.digitalmars.com/d/memory.html#newdelete No need to change this. They just take care of the memory allocation, not the construction of the members. > The current name, "this", reflects to how it is being called from another constructor, which works the same as in Java : So "this()" in java calls "classname()" ? :-S > So if you changed the names, *that* call would look more obscure... ? (since destructors aren't called explicitly, their name matters less) > > Not that it would be *very* strange to instead call eg. "construct()". Right, I'd prefer if the constructor and destructor get treated as normal methods as much as possible. Why change the name? (Ah, because calling another constructor does NOT re-construct the members?) > "this" and "~this" are somewhat obscure names, that much is true... Yeah, I've been programming C/C++ for 8 years now, and I just figured out LAST WEEK that "~class()" could be read as "not class()" :-S By hackers, for hackers. > The only improvement over C++ / Java is not requiring the class name, at the expense of losing "instant recognition" for those used to them... Agreed. So the constructor and destructor should have a fixed name in any class. > For newcomers, "construct" and "destruct" are probably easier to grasp ? For sure they're easier to grasp than this and ~this.. Hey, even !this would be easier to grasp :-) >> (A Python PEP-like system would be very nice for D. All these loose suggestions that get lost in the newsgroup after some time is a real pity. Another suggestion.) > > A list of bugs would be nice too... (like Bugzilla or so) > > But for now, there are just: http://www.prowiki.org/wiki4d/wiki.cgi?PendingPeeves http://www.prowiki.org/wiki4d/wiki.cgi?FeatureRequestList A wiki is not really suitable for this kind of thing becuase of the variable layout. But maybe I'll just update that feature request list. So it doesn't get lost :-/ Lionello. |
December 17, 2004 Re: Rename ctor / dtor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | Lionello Lunesu wrote: > Ah, summing it all up: I think "construct" and "destruct" are the best options too, because... > > * they're actual english words (as opposed to ctor/dtor); > * they actually describe what's going on; > * PHP5 uses them too (be it with "__", which I'd drop); > * they don't cause any conflicts (within D itself that is). PHP adds __ to all system functions, to avoid clashing with legacy names. >>"new" and "delete" are also in use, for allocator/deallocator: >>http://www.digitalmars.com/d/memory.html#newdelete > > No need to change this. They just take care of the memory allocation, not the construction of the members. Just meant that they have less funky names... >>The current name, "this", reflects to how it is being called >>from another constructor, which works the same as in Java : > > So "this()" in java calls "classname()" ? :-S Indeed it does. (and super() calls the parent) In java, the constructor call must be first. (before *any* other code in the constructor) > > A wiki is not really suitable for this kind of thing becuase of the variable layout. But maybe I'll just update that feature request list. So it doesn't get lost :-/ I'd chalk it up to "better than nothing"... --anders |
December 17, 2004 Re: Rename ctor / dtor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | Lionello Lunesu wrote:
> I guess this would lower the threshold for new programmers to use D even more. I hope I don't get only "too late to make such a rigorous change" ractions :-/ (It seems I guess I hope and I think a lot :-S)
Somewhat late as the discussion has been had before. No changes were made then.
Lars Ivar Igesund
|
December 17, 2004 Re: Rename ctor / dtor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars Ivar Igesund | Lars Ivar Igesund wrote: >> I guess this would lower the threshold for new programmers to use D even more. I hope I don't get only "too late to make such a rigorous change" ractions :-/ (It seems I guess I hope and I think a lot :-S) > > Somewhat late as the discussion has been had before. No changes were made then. Do you have any links to any interesting arguments, pro or contra? All I could find was: > I like 'this' it made sense as soon as I heard it. > It's certainly better than typing the class name. > > 'constructor' is too long. > 'ctor' only makes sense if you have heard that term used before. (From http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/7701) Which the names "construct" and "destruct" would fix, IMHO. A little interesting to claim legacy reasons, for a unreleased language? --anders |
December 17, 2004 Re: Rename ctor / dtor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | "Anders F Björklund" <afb@algonet.se> wrote in message news:cpuhd6$1v3e$1@digitaldaemon.com... > Lars Ivar Igesund wrote: > > >> I guess this would lower the threshold for new programmers to use D even more. I hope I don't get only "too late to make such a rigorous change" ractions :-/ (It seems I guess I hope and I think a lot :-S) > > > > Somewhat late as the discussion has been had before. No changes were made then. > > Do you have any links to any interesting arguments, pro or contra? > > > All I could find was: > > > I like 'this' it made sense as soon as I heard it. 1. I always liked "this" too! :) 2. If "this" is a reference to the object, doesn't it make sense that the method that creates the object is also "this"? 3. It is short. 4. It isn't hard to learn or remember. > > It's certainly better than typing the class name. > > > > 'constructor' is too long. > > 'ctor' only makes sense if you have heard that term used before. > > (From http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/7701) > > Which the names "construct" and "destruct" would fix, IMHO. > > > A little interesting to claim legacy reasons, for a unreleased language? > > --anders |
December 17, 2004 Re: Rename ctor / dtor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | Ivan Senji wrote: > 1. I always liked "this" too! :) > 2. If "this" is a reference to the object, doesn't it make sense that the > method that creates the object is also "this"? > 3. It is short. I think in nested constructors make sense, not sure about the otherwise. > 4. It isn't hard to learn or remember. The tricky one is "~this", but that exception could be learned I guess ? It's not like it's going to change, anyway ? ;-) (but if it was, my vote is on: "this"->"construct", "~this"->"destruct") --anders |
December 17, 2004 Re: Rename ctor / dtor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | I also think this() and ~this() are clean, and unabiguous.
They are also very easy to find when reading through source because this is highlighted even for C++ highlighting.
Obviously ctor could be highlighted, but I really don't see an advantage over just using the existing keyword rather than adding another....
Ivan Senji wrote:
> "Anders F Björklund" <afb@algonet.se> wrote in message
> news:cpuhd6$1v3e$1@digitaldaemon.com...
>
>>Lars Ivar Igesund wrote:
>>
>>
>>>>I guess this would lower the threshold for new programmers to use D
>>>>even more. I hope I don't get only "too late to make such a rigorous
>>>>change" ractions :-/ (It seems I guess I hope and I think a lot :-S)
>>>
>>>Somewhat late as the discussion has been had before. No changes were
>>>made then.
>>
>>Do you have any links to any interesting arguments, pro or contra?
>>
>>
>>All I could find was:
>>
>>
>>>I like 'this' it made sense as soon as I heard it.
>
>
> 1. I always liked "this" too! :)
> 2. If "this" is a reference to the object, doesn't it make sense that the
> method
> that creates the object is also "this"?
> 3. It is short.
> 4. It isn't hard to learn or remember.
>
|
December 17, 2004 Re: Rename ctor / dtor | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Medlock | >I also think this() and ~this() are clean, and unabiguous. How can you say it's unambiguous when 'this' is both the constructor method and a pointer to the object? ~this is both the destructor and 'the binary negation of the value of the pointer to the object' (OK, I exagerated, a little)... > They are also very easy to find when reading through source because this is highlighted even for C++ highlighting. True, but hardly an argument worth considering. > Obviously ctor could be highlighted, but I really don't see an advantage over just using the existing keyword rather than adding another.... [copy_paste] * they're actual english words (as opposed to ctor/dtor); * they actually describe what's going on; * PHP5 uses them too (be it with "__", which I'd drop); * they don't cause any conflicts (within D itself that is); * same naming for every class. Compare with against arguments * 'this' highlights in C++ editors; * same naming for every class; *... uh....more? please add. Lionello. |
Copyright © 1999-2021 by the D Language Foundation