Newbie comments about D

Kristian kjkilpi at gmail.com
Thu Aug 17 11:58:25 PDT 2006


I just discovered this very promising language, and decided to write some  
comments about it. This post is lenghtly, so sorry about it. And it's very  
possible that issues discussed below are already handled many times over  
(sorry again, if that's the case). (But hey, you can think me as a fresh  
'test subject' ;) ...)

I have been programming with C++ mainly. (I know a lot of other languages  
as well, though.) Issues that I disliked (or that were problematic) in  
C++ seem to be 'fixed' in D (well, almost all of them; see below). No more  
include files, templates are working fine, compiler generates the  
documentation, etc... just wonderful. And there are property functions,  
invariants, and so on... :)


I was a little surprised that there are no local class objects (put into  
the stack). For example:

struct S {...}
class C {...}

void f() {
     S s;  //s is a local variable
     C c;  //c is NULL, not an object
}

That's not a problem of course. However, it's a bit laborious to write "C  
c = new C;" all the time you need a (local) object. And it's inconsistent  
with other variable declarations.


--- 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.


--- 2 ---

One thing I hate in C++ is that you cannot reimplement a member function  
derived from the base class so that other overload functions won't  
'disappear'. I think this is a well known, and hated, problem in C++. For  
example:

class A {
public:
     virtual void f();
     virtual void f(int);
     virtual void f(float);
};

class B : public A {
public:
     virtual void f();
     //'f(int)' disappears
     //'f(float)' disappears
};

I have never came across to a situation where the other overloads should  
disappear from a deriving class. And, if they should disappear, the  
programmer can still call them via the base class (i.e. "B b;  
b.A::f(10);").

Of course, in D you can use an alias to fix this problem, i.e.:

class B : A {
     void f() {...};
     alias A.f f;
}

But this should not be necessary, IMHO. I would hate to write the aliases  
over and over again...


--- 3 ---

The same goes for the constructors. For example:

class A {
     this() {...}
     this(int) {...}
     this(float) {...}
}

class B : A {
     //all the constructors disappear!
}

This causes a lot of work when you only need to, for example, override one  
little tiny function of the base class.


--- 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;
     ...
}


--- 5 ---

Hmm, could it be possible to automatically add auto/local attribute to  
objects that are not returned (by a function) and that are not assigned to  
anything? For example:

class File {...}  //the file is closed in the destructor

void f() {
     File f = new File;  //auto File f = new File;

     f.open("myfile.txt");
     f.doSomething();
}

Also, auto/local objects could remain in the stack instead of the heap  
(but that's only an issue of optimization).


...

That's all folks, keep up the good work! :)



More information about the Digitalmars-d mailing list