Expanding the horizons of D purity

Timon Gehr timon.gehr at gmx.ch
Fri Nov 1 06:43:57 PDT 2013


On 11/01/2013 01:43 PM, Wyatt wrote:
> On Friday, 1 November 2013 at 11:45:23 UTC, Timon Gehr wrote:
>>
>>     return (y)pure=x=y;
>
> Drifting off topic a little, but how does this expression work? I can't
> recall having seen the (y)pure thing before.
>
> -Wyatt

It is in a comment. The exact expression wouldn't compile in this 
context, (one reason is that I have sloppily left out the parameter 
type, the other is that the body is not considered pure.)

I assume your question extends to the following case which is valid D code?

auto foo(){ return (int y)pure=>2; }

All function attributes (except ref, which I think is a bug in the 
design and/or implementation) can be specified after any parameter list. 
This includes function literal parameter lists (except for the 
single-identifier case).

(y)pure=>2 is a valid template function literal:

auto foo(alias a)(){ return a(3); }
static assert(foo!((y)pure=>2)()==2);
//                 ^~~~~~~~~~

(Typically pure is left out here, except for emphasis, because it is 
inferred anyway, so one would rather use y=>2.)


More information about the Digitalmars-d mailing list