May 29, 2012 Re: Purity in D – new article | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | 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 |
May 30, 2012 Re: Purity in D – new article | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Tuesday, 29 May 2012 at 18:46:21 UTC, Ali Çehreli wrote:
> 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.
Okay, thanks for the feedback – will see about clarifying the section.
David
|
May 30, 2012 Re: Purity in D – new article | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | On 29/05/12 19:35, David Nadlinger wrote: > On Tuesday, 29 May 2012 at 12:08:08 UTC, Don Clugston wrote: >> And to set the record straight -- the relaxed purity ideas were not my >> idea. >> I forget who first said them, but it wasn't me. I just championed them. > > Unfortunately, I don't quite remember either – was it Bruno Medeiros? In > any case, if somebody can help my memory here, I'd be glad to give > credit to the one who came up with the original proposal in the article > as well. > > David The successful proposal, using "weakly pure/strongly pure" (Sep 21 2010): http://www.digitalmars.com/d/archives/digitalmars/D/Proposal_Relax_rules_for_pure_117735.html It"s basically the same as this one by Bruno (Apr 29 2008), which uses "partially pure" and mentions an earlier post by me: http://www.digitalmars.com/d/archives/digitalmars/D/Idea_partially_pure_functions_70762.html#N70762 And the earliest reference I could find is by me (Apr 5 2008) where I called it an "amoral" function. http://www.digitalmars.com/d/archives/digitalmars/D/Grafting_Functional_Support_on_Top_of_an_Imperative_Language_69253.html The first compiler release with pure function attributes (though not implemented) was released in Apr 22, 2008 and the first with pure as a keyword was Jan 20 2008. So surely this is close to the original. So now I'm confused, maybe it *was* me after all!???? Then formalized by Bruno, and later championed by me? |
May 30, 2012 Re: Purity in D – new article | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | On 27 May 2012 21:56, David Nadlinger <see@klickverbot.at> 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 Enjoyable read. :) Makes me think about getting round to writing something someday... -- Iain Buclaw *(p < e ? p++ : p) = (c & 0x0f) + '0'; |
Copyright © 1999-2021 by the D Language Foundation