Types of lambda args

Cecil Ward cecil at cecilward.com
Wed Aug 26 15:57:37 UTC 2020


On Monday, 17 August 2020 at 04:30:08 UTC, H. S. Teoh wrote:
> On Mon, Aug 17, 2020 at 12:20:24AM +0000, Cecil Ward via 
> Digitalmars-d-learn wrote:
>> In a lambda, how do we know what types the arguments are? In 
>> something
>> like
>>     (x) => x * x
>
> It's implemented as a template, whose argument types are 
> inferred based on usage context.
>
>
>> - there I just don’t get it at all. Can you write
>>     (uint x) => x * x
>
> Of course you can.
>
>
>> I’m lost.
> [...]
>
> If you're ever unsure of what the inferred type(s) are, you can 
> do replace the lambda with something like this:
>
> 	(x) { pragma(msg, typeof(x)); return x*x }
>
> which will print out the inferred type when the compiler 
> instantiates the lambda.
>
>
> T

Ah! That’s the vital missing piece - I didn’t realise it was like 
a template - I just thought it was an ordinary plain anonymous 
function, not a generic. All makes sense now.




More information about the Digitalmars-d-learn mailing list