How does D improve design practices over C++?

Tony tonytech08 at gmail.com
Thu Nov 6 13:18:50 PST 2008


"Robert Fraser" <fraserofthenight at gmail.com> wrote in message 
news:gergmk$107u$1 at digitalmars.com...
> Tony wrote:
>>>>> - Modules
>>>> If that means doing away with header files, I don't think I like it. I 
>>>> rely
>>>> on headers as the engineer's blueprint (of course you have to write 
>>>> very
>>>> clean code to have that make sense).
>>>>
>>> You missed that one, again. You can have headers in D, too.
>>
>> Yes, I don't really know "modules" other than what they obviously imply: 
>> separate compilation units/namespaces?
>
> It means you don't have to specify everything 2 times if you don't want to 
> (you can if you feel the need). So if you change a function's signature, 
> you just friggin' change it, you don't change it at the function and in 
> the header.

I like the header as the high level view of the code, be it classes or 
functions or whatever.

> This also makes compilation times much faster, since the header files 
> don't need to be included & recompiled during every compilation -- if 
> you're compiling a group of files that depend on one another at once, the 
> compiler will only compile each one once.

Compile times as I am not doing large scale development. And with so much 
processor power available these days, I really don't see a problem with 
compile times. Some care in laying out code and headers goes a long way.

> And, yes, better/automated namespacing.

If I was using other peoples' code or language standard library code, 
namespaces could be mildly convenient. As it is though, I'm not doing either 
of those things.

>
>>>>> - Garbage collection
>>>> That's a major deal breaker for me.
>>>>
>>> You can turn it off and do the memory management by yourself, if you 
>>> wish:
>>>
>>> struct Foo {}
>>> Foo* foo = new Foo();
>>> delete foo;
>>
>> And if I don't want to use new and delete?
>
> Then you use the GC. Or malloc/free. Or placement new. Or... seriously, 
> what do you want here?

OK. I was just wondering how C++ like those things were in D. Apparently 
pretty much the same, if not exactly so even.

>
>>>>> - Delegates (Specifically, encouraging there use by making them simple 
>>>>> to
>>>>> use)
>>>> Can't comment.
>>>>
>>> This is an awesome feature, too bad you don't have this one in C++. You 
>>> will like it, believe me!
>>
>> Well what the heck is it?!
>
> It's a function pointer with context. So you can point to a member 
> function, for example.  In D2, there's also closures so...
>
> void main(string[] args)
> {
>     auto dg = getADelegate();
>     writefln("%d", dg());
>     writefln("%d", dg());
>     writefln("%d", dg());
> }
>
> int delegate() getADelegate()
> {
>     int i = 0;
>     return delegate int() { return ++i; };
> }
>
> Will yeild:
> 1
> 2
> 3

In C++:

int GetAnInt()
{
    static int i = 0;
    return ++i;
}

So what kind of major programming problems do delegates and closures solve? 
Are they just syntactic sugar?

Tony 





More information about the Digitalmars-d mailing list