January 17, 2012 Meaning of pure member function | ||||
---|---|---|---|---|
| ||||
The following code compiles without error: class C { int x; // what does 'pure void' mean?? pure void f() { x++; // why is this legal? } } What does 'pure' mean when applied to a member function? Based on Andrei's book, 'pure' means that the function's result depends only on its input. And based on the fact this code compiles, I deduced that 'this' is included as part of the function's input. However, the function is clearly changing one of its inputs (changing a member of 'this'). Furthermore, what on earth is 'pure void' supposed to mean and why does the compiler accept it? Changing the function to read: pure int f() { return x++; } also compiles without any complaint from the compiler. Yet calling writeln() from within f() produces an error. Why? T -- Computerese Irregular Verb Conjugation: I have preferences. You have biases. He/She has prejudices. -- Gene Wirchenko |
January 17, 2012 Re: Meaning of pure member function | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Tuesday, 17 January 2012 at 05:16:33 UTC, H. S. Teoh wrote: > The following code compiles without error: > > class C { > int x; > > // what does 'pure void' mean?? > pure void f() { > x++; // why is this legal? > } > } > > What does 'pure' mean when applied to a member function? This is a weakly pure function usable by strongly pure functions. Namely it is a helper function to those that can claim to be strongly pure. Maybe bearophile's blog will shed some light: http://leonardo-m.livejournal.com/99194.html Or stackoverflow: http://stackoverflow.com/questions/5812186/pure-functional-programming-in-d > Furthermore, what on earth is 'pure void' supposed to > mean and why does the compiler accept it? Well it can only be useful as a weakly pure function as those are allowed to modify their arguments. In any case, if the function was strongly pure: pure void foo() {} any call to it would just be eliminated as having no side effects. |
Copyright © 1999-2021 by the D Language Foundation