pure or not pure?

Georg Wrede georg at nospam.org
Wed Apr 9 14:42:28 PDT 2008


Steven Schveighoffer wrote:
> "Janice Caron" wrote
> 
> So how is new char[] a pure function?  And why can't I 'wrap' new?  For 
> instance, if I have a function:
> 
> int[] createIncreasingSequence(int s, int e, int step){...}
> 
> which creates an array of ints that start at s and end at e, increasing by 
> step each time, why can't I make that pure?  i.e. I want to specify my own 
> way to initialize an array.  If I do that in 10 different pure functions, 
> you're saying I have to re-implement that code in each one?

The return value must be immutable if you're going to use it several times.

And if you want to cache the return values outside of the function (as 
in only returning a new array when new parameters are used, and 
otherwise just a reference to a previously returned one), then I guess I 
don't have an opinion. :-?

> This was misleading on my part.  I should have asked instead of these two 
> questions, if substr is allowed to be pure, can it be called from a non-pure 
> function?  In the context of being called from a pure function, src is 
> unique, because pure has to have unique data.  But in the context of calling 
> from a non-pure function, it might not be...

Ultimately, any function is called from main(). Which is a non-pure 
function.




More information about the Digitalmars-d mailing list