Is this function pure?

Don Clugston dac at nospam.com.au
Tue Sep 18 07:58:06 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;
> 	}
> }
> 
> 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?

Interesting question.
This is exactly the same:

int probablyToo(char [] x)
{
       try {
          return (x ~ "a").length;
       }
       catch {
          return 0;
       }
}

If 'new' was forbidden for reasons of non-determinism, then string concatenation 
would have to be, as well. Which I think would make pure functions pretty 
limited. But, if you assume that anything with is CTFE-able is also pure, then 
string concatenation is OK.



More information about the Digitalmars-d mailing list