Pure functions in D

Leandro Lucarella llucax at gmail.com
Sat Sep 27 15:32:56 PDT 2008


Jason House, el 27 de septiembre a las 07:32 me escribiste:
> Walter Bright Wrote:
> 
> > Bruno Medeiros wrote:
> > 
> > > Will memory allocation be considered pure?
> > 
> > I don't know yet. If memory allocation failure is a non-recoverable 
> > exception, then pure functions can allocate memory.
> 
> Pure functions allocating memory is a nice feature to have. A lot of D code wants to allocate memory.  It would certainly allow more return values from pure functions. I don't know what impacts that'd have on the memory model. I've had the impression that parallelizing pure functions could be lockless.

But I don't see how a function can be pure if allocates, for example:

pure Image rotate(Image original)
{
	Image img = new Image(original.size)
	// some rotation logic
	return img;
}

If this "pure" function is called with enough memory, it will return
a copy of the original image, rotated. If there is no enough memory, the
function will throw (note that the input is exactly the same for both
calls, but the result is different).

You can add some try/catch to make it nonthrow, but if you have no memory,
you can't possibly return the same image as it would be returned when
memory is available.

What about stack allocation? I don't even know what's the current
behaviour. Can you get out of stack? Is an exception raised? The program
aborted? Anything else?


This is a really tricky issue indeed, because not being able to allocate
in a pure function would reduce their utility greatly.


-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
El techo de mi cuarto lleno de cometas



More information about the Digitalmars-d mailing list