Is this function pure?

Vladimir Panteleev thecybershadow at gmail.com
Tue Sep 18 06:21:10 PDT 2007


On Tue, 18 Sep 2007 15:36:50 +0300, Janice Caron <caron800 at googlemail.com> wrote:

> Is this function pure?
>
> int probablyNotPure(int x)
> {
> 	try
> 	{
> 		char[] a = new char[x];
> 		return a.length;
> 	}
> 	catch
> 	{
> 		return 0;
> 	}
> }
>
> I must assume not, since it's not deterministic. But which part of it
> wrong? Should "new" be forbidden? Or is "try/catch"? Or both?

Forbidding operations that may cause an exception would imply forbidding division, since that may raise a division by zero statement. I don't see what's wrong with handling your own exceptions. And, "determinism" is a stretchable notion - if you can determine the number of concurrent threads which will call this function, the points where switches occur and the amount of available memory... in D's context, though, I don't think determinism should be a necessity for pure functions - just the limitation of not accessing external data or "non-pure" code.

-- 
Best regards,
 Vladimir                          mailto:thecybershadow at gmail.com



More information about the Digitalmars-d mailing list