www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why don't Function Lambdas Cast to Delegate?

reply Q. Schroll <qs.il.paperinik gmail.com> writes:
Why don't lambdas cast to a delegate if they are of type R 
function(Args)? I don't see any reason to that; a lambda should 
be a delegate type by default, and a function only as a special 
guarantee/optimization. It just makes them cumbersome to use with 
toDelegate.
Probably there is a good reason why R function(Args) does not 
implicitly cast to R delegate(Args); I can imagine something 
internally (memory layout etc.) causes that. I'd just like to 
know.
Nov 21 2016
next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 11/21/16 11:24 AM, Q. Schroll wrote:
 Why don't lambdas cast to a delegate if they are of type R
 function(Args)? I don't see any reason to that; a lambda should be a
 delegate type by default, and a function only as a special
 guarantee/optimization. It just makes them cumbersome to use with
 toDelegate.
 Probably there is a good reason why R function(Args) does not implicitly
 cast to R delegate(Args); I can imagine something internally (memory
 layout etc.) causes that. I'd just like to know.
The ABI for function calls is different than the ABI for delegate calls. I believe this is a necessary limitation due to ABIs on some platforms, but I'm not 100% sure. It could just be a legacy issue. -Steve
Nov 21 2016
prev sibling parent Rene Zwanenburg <renezwanenburg gmail.com> writes:
On Monday, 21 November 2016 at 16:24:38 UTC, Q. Schroll wrote:
 Why don't lambdas cast to a delegate if they are of type R 
 function(Args)? I don't see any reason to that; a lambda should 
 be a delegate type by default, and a function only as a special 
 guarantee/optimization. It just makes them cumbersome to use 
 with toDelegate.
 Probably there is a good reason why R function(Args) does not 
 implicitly cast to R delegate(Args); I can imagine something 
 internally (memory layout etc.) causes that. I'd just like to 
 know.
A delegate has the hidden context pointer as parameter in addition to the 'visible' parameters. That causes functions and delegates with the same declared params to look different on the ABI level.
Nov 21 2016