Infer purity of functions too?

Jonathan M Davis jmdavisProg at gmx.com
Mon Jul 18 16:43:38 PDT 2011


On 2011-07-18 16:07, bearophile wrote:
> > Example: if a not pure function sqr is called by both a not pure template
> > bar and by a pure function foo, the compiler raises an error in foo,
> > because sqr is not pure, but compiles the pure main because sqr called
> > by bar is seen as pure :-)
> 
> This seems quite useful for delegates given to higher order functions:
> 
> int spam(in int x) pure { return x * 3; }
> auto squares = map!((int x){ return spam(x) * x; })(iota(10));
> 
> With this idea squares is usable in a pure function too, because the map
> template infers the given delegate as pure.
> 
> Otherwise you need to write:
> 
> int spam(in int x) pure { return x * 3; }
> auto squares = map!((int x) pure { return spam(x) * x; })(iota(10));

I believe that there is some level of purity inference with delegates, but I 
don't know what the situation with that is exactly.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list