Newbie comments about D

Kristian kjkilpi at gmail.com
Fri Aug 18 11:45:39 PDT 2006


On Fri, 18 Aug 2006 00:24:44 +0300, Reiner Pope  
<reiner.pope at REMOVE.THIS.gmail.com> wrote:
> Kristian wrote:
>>  --- 1 ---
>>  Well, how about this:
>>  The compiler could initialize a variable with a new object or NULL  
>> depending on if a value is assigned to it before it's used. For example:
>>  void f() {
>>     C c1;  //c1 = new C;
>>     C c2;  //c2 = NULL;
>>      if(c1.f() == 1) {...}
>>      c2 = getSomeObject();
>> }
>>  This unifies all the variable declarations, and you don't need to  
>> write long " = new ..." initializations!
>>  This also means, of course, that an object is always initialized for  
>> you (as it should, you have provided a constructor for it, haven't  
>> you?). The compiler just optimized it's initial value, i.e. a new  
>> object is not allocated when it's unnecessary.
>
> The problem is that the compiler can't tell. While it's possible in  
> trivial cases, one can write arbitrarily complex code, which means the  
> compiler needs to be sufficiently smart to be sure. Walter has referred  
> to this problem before with initialization, and the fact that it is  
> annoying when the compiler is stupid and gets it wrong.

Ok, I hear you. I'm accustomed to my C++ compiler saying "warning: local  
variable X used without having been initialized". Maybe the compiler  
misses some bizarre cases. Hey, lets hope that dmd/dmc will also use such  
warnings some day... ;)

However, if the compiler is unsure which init value it should use, then a  
new object should been used. It should be easy to make a compiler smart  
enough that it gets almost all the cases right. If you write some extreme  
code, or the code is very time critical (and you're unsure how well the  
compiler will do), then you can define that the object must be initialized  
with NULL (e.g. "C c = NULL;").

The language should be constructed for common cases, not for some possible  
situations that happen rariry. Those rare cases can be more or less  
awkward to write.


>>   --- 4 ---
>>  By the way, the 'auto' attribute is used with local variables, so  
>> would a word 'local' be a more logical choice?
>>  void f() {
>>     local C c = new C;
>>     ...
>> }
> A huge change of code would be required for that.

For greater (and future) good perhaps...? ;)

Well, such changes can be done automatically, can't they? (Well, at least  
I have such a search & replace program (developed by me).)

One solution could be that there would be two different words for the same  
attribute (auto & local/scope). Of course, that's not an optimal solution,  
but eventually "auto" would wither away (and new people won't even know  
"auto").



More information about the Digitalmars-d mailing list