2

What does it mean for a pure function to return pure?

pure int doubleMe(in int i) pure { return i * 2; }

The code compiles without giving redundant storage class pure, so I suppose this is not a bug?

// DMD 2.056

Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
Arlen
  • 6,641
  • 4
  • 29
  • 61
  • I've been trying to clean up the [tag:pure] tag, because it sometimes refers to pure virtual functions, sometimes to [pure](http://beebole.com/pure/) and sometimes to [pure](http://en.wikipedia.org/wiki/Pure_(programming_language)) - among others. But I don't know anything about [tag:d]. Could you confirm if my tag edit is appropriate? Would [tag:purely-functional] work for this question - I created [tag:pure-function], so if [tag:purely-functional] works I think it would be better to use the existing tag. – Richard JP Le Guen Mar 14 '13 at 18:31
  • @RichardJPLeGuen Pure as in [functional purity](http://en.wikipedia.org/wiki/Pure_function), so pure-function would work for this question. purely-functional, not so much. – Arlen Mar 14 '13 at 19:22

2 Answers2

4

Pure is a function attribute. Function attributes can go before the return type or after the parameter list. It describes the function, not the return type, in both cases. Hence, there is no such thing as "returning pure".

It should raise an error, it's probably a bug. The same thing is ignored with some other attributes too, like @safe.

jA_cOp
  • 3,275
  • 19
  • 15
0

pure int probably should be ignored. However, I believe the reason why we have it is that a function may return a "pure function". In that case I think it is arguable whether it is a bug or not.

DejanLekic
  • 18,787
  • 4
  • 46
  • 77
  • `pure` put before a return type always refers to the function. This is the same as with storage classes and other function attributes. – jA_cOp Dec 13 '11 at 06:59