What's wrong with D's templates?

Walter Bright newshound1 at digitalmars.com
Tue Dec 22 01:33:51 PST 2009


grauzone wrote:
> Walter Bright wrote:
>> I can expound on the huge advantages immutability and purity offer, if 
>> you want.
> 
> Yes, I'd like to hear about this. You can leave away the things that are 
> not going to be implemented (like memorization of pure return values), 
> are only micro-optimizations (common sub-expression elimination with 
> pure functions?), which don't work (immutable was used to make strings 
> read-only, but you can stomp over immutable arrays), which were thought 
> to be useful, but nothing has materialized yet (something like immutable 
> was supposed to be the cure for multithreading)...

The short answer is: the benefits of functional programming.

The longer answer:

1. optimizations - yes, the optimizer does take advantage of 
immutability and purity, and yes, those optimizations are minor. But 
they do exist and do work.

2. immutable reference types can be treated as if they were value types 
- this advantage is most obvious in strings. Without immutability (and 
this happens in C, C++, and D1) programmers tend to make their own 
private copies of things "just in case" someone else changes them.

3. (2) has a great advantage in doing message passing between threads. 
This model was popularized by Erlang, and is very successful. You can do 
message passing without immutable references, but you've got to hope and 
pray that your programming team didn't make any mistakes with it. With 
immutable references, you have a statically enforced guarantee. Value 
types (and immutable references) do not need synchronization.

4. immutability and purity enable user reasoning about a program. 
Otherwise, you have to rely on the (probably wrong) documentation about 
a function to see what its effects are, and if it has any side effects.

Yes, there was a recently discovered bug which enabled modifying an 
immutable array. This was a bug, and has been fixed. A bug does not mean 
the concept is broken.


> Over two years have passed since immutable/const was added to dmd, but I 
> couldn't see any benefit yet. But lots of new compiler bugs. There's the 
> danger that those feature are all nothing but hot air in real 
> programming. I like D, and it sure would relief me to hear that this is 
> not the case.

The message passing threading advantage awaits the construction of a 
message passing library. Sean Kelly is working on it.



More information about the Digitalmars-d mailing list