www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Rename ctor / dtor

reply "Lionello Lunesu" <lionello.lunesu crystalinter.remove.com> writes:
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/ 
Dec 17 2004
next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
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
Dec 17 2004
parent reply "Lionello Lunesu" <lionello.lunesu crystalinter.remove.com> writes:
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.
Dec 17 2004
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
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
Dec 17 2004
parent "Lionello Lunesu" <lionello.lunesu crystalinter.remove.com> writes:
"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...
Yeah, indeed. They should have been called "mem" and "~mem". It' short, always the same, easy to remember. I'm joking! Djeez. L.
Dec 17 2004
prev sibling next sibling parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
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
Dec 17 2004
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
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
Dec 17 2004
next sibling parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"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
Dec 17 2004
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
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
Dec 17 2004
prev sibling parent reply David Medlock <amedlock nospam.org> writes:
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.
Dec 17 2004
parent reply "Lionello Lunesu" <lionello.lunesu crystalinter.remove.com> writes:
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.
Dec 17 2004
parent reply David Medlock <amedlock nospam.org> writes:
Lionello Lunesu wrote:

 
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.
Adding more keywords adds the the number of things I must keep in my head in addition to the program I am working on. Every bit of baggage added on increases complexity, and pushes you towards the bucket of trash that is C++. If you disagree fine, but I don't think my point is 'hardly 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.
In your arguments for using constructor I can apply most of them to *this* also: - *this* is an english word, and its shorter than constructor and destructor. - this() describes construction (imo) - PHP uses them??? Python uses __init__, so why not use init() ? - *this doesnt cause any naming conflicts - this allows same naming for every class/no retyping when renaming class I still do not see how this and ~this are not readable. I learned them both as soon as I read the spec. Yes Walter *could* change the syntax, but I still don't see *why* he should. If its not broken....
Dec 17 2004
parent reply "Lionello Lunesu" <lionello.lunesu crystalinter.remove.com> writes:
 Adding more keywords adds the the number of things I must keep in my head 
 in addition to the program I am working on.  Every bit of baggage added on 
 increases complexity, and pushes you towards the bucket of trash that is 
 C++.
 If you disagree fine, but I don't think my point is 'hardly worth 
 considering'.
Hey, but that's another point! I was reacting to the "works with C++ syntax highlighting"-point. As for the "number of words"-point, that's actually a valid point! Not much I can say. Add it to the list, I suppose. But IMO (Did you see? The H's get dropped as the discussion intensifies) keeping track of two meanings of one word is more difficult than having two different words for the different meanings. Actually three different words (this/construct*/destruct*), against two (this/this/~this).
 In your arguments for using constructor I can apply most of them to *this* 
 also:

 -  *this* is an english word, and its shorter than constructor and 
 destructor.
That's 2 valid points
 -  this() describes construction (imo)
:-S I'd like to see a vote on this one
 -  PHP uses them??? Python uses __init__, so why not use init() ?
Agreed, lets add it to the list of alternatives.
 - *this  doesnt cause any naming conflicts
Ah, but it does! Remember 'this' the pointer?
 - this allows same naming for every class/no retyping when renaming class
This one is definately important and should apply to any alternative too.
 I still do not see how this and ~this are not readable.  I learned them 
 both as soon as I read the spec.  Yes Walter *could* change the syntax, 
 but I still don't see *why* he should.

 If its not broken....
I hate that line... :-( Also it keeps people from refactoring their code 'cause "it works, right?".. In the software product I'm currently working on, this reasoning caused too many problems. (Hmm, voting with a wiki, everybody add 1, and only 1, to the number of votes for your favorite alternative :-S) L.
Dec 17 2004
parent reply David Medlock <amedlock nospam.org> writes:
Lionello Lunesu wrote:
I wrote: 
In your arguments for using constructor I can apply most of them to *this* 
also:

-  *this* is an english word, and its shorter than constructor and 
destructor.
That's 2 valid points
-  this() describes construction (imo)
:-S I'd like to see a vote on this one
Hehe I will agree for some its not really descriptive.
 
 
-  PHP uses them??? Python uses __init__, so why not use init() ?
Agreed, lets add it to the list of alternatives.
That was tongue in cheek! PHP seems an odd animal to mimic for a systems language.
 
 
- *this  doesnt cause any naming conflicts
Ah, but it does! Remember 'this' the pointer? L.
I actually like the suggestion you made in the other thread class() and ~class(), but its too late at this point.
Dec 17 2004
parent Lionello Lunesu <Lionello_member pathlink.com> writes:
Yo,

-  PHP uses them??? Python uses __init__, so why not use init() ?
Agreed, lets add it to the list of alternatives.
That was tongue in cheek! PHP seems an odd animal to mimic for a systems language.
They're still just english words, but PHP just proves that it kind-a makes sense to call the constructor and destructor 'construct' and 'destruct'. Not really mimicing a PHP language feature..
I actually like the suggestion you made in the other thread
class() and ~class(), but its too late at this point.
Yeah, I guess that's really the only thing "this" and "~this" have going for them: it was decided, so live with it :-S I had another reason though why "this" is a strange name for the constructor. One pro was that "this" had something to do with the creation of the object (be it vaguely). But for "static this", the static constructor, it doesn't make any sense, since there's no 'this' being initialised. Lionello.
Dec 18 2004
prev sibling parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
Anders F Björklund wrote:
 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?
Nah, I like the current naming :) (although I have no really strong opinions on this matter) Lars Ivar Igesund
Dec 18 2004
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Lars Ivar Igesund wrote:

 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?
Nah, I like the current naming :) (although I have no really strong opinions on this matter)
Okay, so people wanted the naming to change before - and it didn't... And because it was left as it is then, that's the way it's gonna be ? I can live with "this" and "not this", just thought the new names "construct" and "destruct" were more self-explanatory than they are ? Maybe for 2.0 then. --anders
Dec 18 2004
parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
Anders F Björklund wrote:
 Lars Ivar Igesund wrote:
 
 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?
Nah, I like the current naming :) (although I have no really strong opinions on this matter)
Okay, so people wanted the naming to change before - and it didn't... And because it was left as it is then, that's the way it's gonna be ?
Well, people don't necessarily mean 'all'. I don't think enough people could agree on an better alternative. The fact that possibly most people on the NG might believe that there are better alternatives, don't mean that such an alternative got a major number of votes when measured up against no real need for change. Lars Ivar Igesund
Dec 18 2004
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Lars Ivar Igesund wrote:

 Well, people don't necessarily mean 'all'. I don't think enough people 
 could agree on an better alternative.  The fact that possibly most 
 people on the NG might believe that there are better alternatives, don't 
  mean that such an alternative got a major number of votes when measured 
 up against no real need for change.
Fair enough... And there are enough other issues to address, I suppose. --anders
Dec 18 2004
prev sibling next sibling parent reply J C Calvarese <jcc7 cox.net> writes:
In article <cpu62e$1i7f$1 digitaldaemon.com>, Lionello Lunesu says...
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(...) ?
It's always short. It's always the same. It's a keyword, but it's not an additional keyword. ..
Would changing the names to "constructor" and "destructor" (or "ctor" and 
"dtor", or "construct" / "destruct") not make more sense?

</suggestion>
This has been suggested before. I think it's much too late to change it now. http://www.digitalmars.com/d/archives/637.html http://www.digitalmars.com/d/archives/14847.html jcc7
Dec 17 2004
parent reply "Lionello Lunesu" <lionello.lunesu crystalinter.remove.com> writes:
 It's always short. It's always the same.
True, but this doesn't really 'fire' the alternatives.
 It's a keyword, but it's not an additional keyword.
I regard that as a bad thing. Agreed, 'this' as constructor has something to do with the other meaning of 'this'. But why not call it "class()" and "~class()" instead? Still short, still the same, no real conflict (since you can't evaluate "class" the way you can evaluate both this'es)
 This has been suggested before. I think it's much too late to change it 
 now.
So D's fixed the way it is? I'm actually asking.
 http://www.digitalmars.com/d/archives/637.html
 http://www.digitalmars.com/d/archives/14847.html
As "Ric" said it:
Oh well, maybe I'm way too late for such a proposal, but anyway I 
tried... Lionello.
Dec 17 2004
parent J C Calvarese <jcc7 cox.net> writes:
In article <cpuu6m$2ds3$1 digitaldaemon.com>, Lionello Lunesu says...
 It's always short. It's always the same.
True, but this doesn't really 'fire' the alternatives.
Okay, but the alternative hasn't even been hired yet, and the status quo already all of his stuff in his office.
 It's a keyword, but it's not an additional keyword.
I regard that as a bad thing. Agreed, 'this' as constructor has something to do with the other meaning of 'this'. But why not call it "class()" and "~class()" instead? Still short, still the same, no real conflict (since you can't evaluate "class" the way you can evaluate both this'es)
I prefer "this". "class" is too long. ;)
 This has been suggested before. I think it's much too late to change it 
 now.
So D's fixed the way it is? I'm actually asking.
Only Walter knows. ;) I think it mostly is set in stone. I was under the impression that once he gets the bugs out, Walter is going to stamp 1.00 on it. Hopefully, that'll be soon. It's not a good time to be "fixing" what isn't "broken". Maybe it's just me, but this and ~this doesn't seem that hard to remember or understand. (Of course, I'm the one who was trying to use apostrophes to comment out code yesterday, so maybe I shouldn't be voicing my opinion. Oops.) Also, I'm sure Walter has seen suggestions about this before. So I think if he was going to change it, he would have done so last year.
As "Ric" said it:

Oh well, maybe I'm way too late for such a proposal, but anyway I 
tried... Lionello.
It doesn't hurt to mention it, but I doubt it's going to change. jcc7
Dec 17 2004
prev sibling parent Ant <Ant_member pathlink.com> writes:
In article <cpu62e$1i7f$1 digitaldaemon.com>, Lionello Lunesu says...
Hi..

<preamble>

How is D's this(...) and ~this(...) for the constructor and destructor for a 
this come up before. I fully support it. Walter completly ignores it. this() and ~this and ctor and dtor make absolutly no sence. my last argument after exausting all valid ones that are surfacing again (I'm not sure I made this public) is that the parser on phobos calls this() "ctor" and ~this() "dtor". but we are talking to a wall. Ant
Dec 17 2004