Thread overview
Immutable
Mar 27, 2021
Brad
Mar 27, 2021
bachmeier
Mar 27, 2021
Meta
March 27, 2021
I was looking through lots of sample code on Rosetta Code.  D has a lot of solutions out there.  That is really nice but it has me wondering - coming from other languages that do not support the concept of immutability - do real world programmers and/or hobbyists really use it as much as I see it on Rosetta Code?  I know it adds a layer of security to your code, but I am still thinking "why?".

Thanks for entertaining a newbie question.
March 27, 2021
On Saturday, 27 March 2021 at 20:44:12 UTC, Brad wrote:
> I was looking through lots of sample code on Rosetta Code.  D has a lot of solutions out there.  That is really nice but it has me wondering - coming from other languages that do not support the concept of immutability - do real world programmers and/or hobbyists really use it as much as I see it on Rosetta Code?  I know it adds a layer of security to your code, but I am still thinking "why?".
>
> Thanks for entertaining a newbie question.

I'm not enough of an expert on immutable to give an informed opinion (I don't care for the implementation). Jonathan Davis has written this blog post about the limitations of const, which also talks about immutable quite a bit:

http://www.jmdavisprog.com/articles/why-const-sucks.html
March 27, 2021
On Saturday, 27 March 2021 at 20:44:12 UTC, Brad wrote:
> I was looking through lots of sample code on Rosetta Code.  D has a lot of solutions out there.  That is really nice but it has me wondering - coming from other languages that do not support the concept of immutability - do real world programmers and/or hobbyists really use it as much as I see it on Rosetta Code?  I know it adds a layer of security to your code, but I am still thinking "why?".
>
> Thanks for entertaining a newbie question.

FYI, most of those examples were written by someone who goes by "Bearophile", and their style of writing D uses immutable/const/pure/nothrow/etc. wherever possible. It's not necessarily the style that all D programmers use, though there are a number of people that do.

As for advantages, when it comes to the basic value types (int, float, bool, etc.), it's not all that useful. Where immutable can become very useful, though, is with types that have indirections (i.e. pointers). D's `string` type, for example, is actually a simple alias in Druntime:

alias string = immutable(char)[];

Meaning "a mutable array of immutable chars". This means that you can modify the array itself, such as changing its length, appending to it, etc., but you cannot its individual elements. E.g., the following will work:

string s1 = "this is a string";
s1 ~= '.';
assert(s1 == "this is a string.");

But this will NOT work:

string s2 = "this is a string ";
s2[$ - 1] = '.'; Error: cannot modify immutable expression s2[__dollar - 1LU]

This is just a simple example, but it allows the compiler to do some major optimizations on string handling code. Something that is usually very slow in C/C++ is such code, because strings are mutable arrays of mutable characters. Thus, they have to be copied around everywhere, which is slow and uses a lot of memory unnecessarily.

Not so in D; because strings are mutable arrays of immutable characters, you can freely pass out references to the whole string, or a subrange of it. This would be very dangerous in C/C++, but in D you don't have to worry about the string changing out from under you by some code somewhere else in the program that you gave a reference to. Thus, string handling code is far faster in D than in C/C++.

That is just one example, but you can see how immutable makes possible a lot of optimizations that would simply be impossible in languages without it.