On 05/27/2012 01:56 PM, David Nadlinger wrote:
Some of you might remember that I have been meaning to write a
comprehensive introduction to design and use of purity for quite some
while now – I finally got around to do so:

http://klickverbot.at/blog/2012/05/purity-in-d/

Feedback and criticism of all kinds very welcome!

David

Thank you very much for the article, which should already be translated to other languages. ;)

In addition to having a much clearer idea on the topic after reading your article, I have also been pleasantly surprised that one of my problems with the type system has also been solved by 'pure'.

I had posted about this topic on the d.D.learn forum before, that the return value of the following function should implicitly be convertible to immutable:

import std.stdio;

char[] repeat(dchar d, size_t n)
{
    char[] result;

    foreach (i; 0 .. n) {
        result ~= d;
    }

    return result;
}

void main()
{
    char[] c = repeat('-', 2);
    string s = repeat('=', 3);  // <- compilation error
}

Now I see that my problem has been not marking repeat() as 'pure'. The following works. Awesome! :)

char[] repeat(dchar d, size_t n) pure
{
    // ...
}

However, I would like to make a suggestion about that section in the article. Under the "pure and immutable – again?" section, you say

    "The effects of const and immutable on referential
     transparency have already been discussed at length"

and then continue with

    "the return value of pure functions can in some cases be
     safely cast to immutable".

That gave me the impression that the implicit conversion to immutable of the return value would still work even with the following definition of primes():

ulong[] primes(uint n, immutable char[] i, const char[] c)
{
    // ...
}

Unfortunately, that does not compile with dmd 2.059 and I think that I understand why. Would that be possible for you to make it clear under what conditions the return value can be converted to immutable.

Once again, thank you very much for a great article,
Ali

--
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html

Reply via email to