Is this function pure?

0ffh spam at frankhirsch.net
Tue Sep 18 11:31:31 PDT 2007


Janice Caron wrote:
> Is this function pure?
> 
> int probablyNotPure(int x)
> {
> 	try
> 	{
> 		char[] a = new char[x];
> 		return a.length;
> 	}
> 	catch
> 	{
> 		return 0;
> 	}
> }

Actually, in spite of all the "Not quite sure, probably not." replies
you had so far, I want to state boldly: Yes, sure, why not? :-)

Scared by memory allocation changing the state of the GC?
You may give a damn: When the function leaves, the dynamic array is
dead, and thus not part of the state any more.

Functional purity has only these conditions:

* The result is a function of the parameters, and only of the parameters.
* Variables can be created and duplicated, but not changed.

I don't see your example violating these conditions.

Regards, frank





More information about the Digitalmars-d mailing list