We need to have a way to say "convert this nested function into a struct"

via Digitalmars-d digitalmars-d at puremagic.com
Sat Jun 6 08:24:33 PDT 2015


On Saturday, 6 June 2015 at 13:44:51 UTC, Jacob Carlborg wrote:
> On 2015-06-06 08:59, Jonathan M Davis wrote:
>> On Saturday, 6 June 2015 at 06:16:17 UTC, Andrei Alexandrescu 
>> wrote:
>>> Nested functions that allocate their environment dynamically 
>>> can be
>>> quite useful. However, oftentimes the need is to convert the 
>>> code plus
>>> the data needed into an anonymous struct that copies the 
>>> state inside,
>>> similar to C++ lambdas that capture by value.
>>>
>>> I wonder how to integrate that within the language nicely.
>>
>> Some of us were discussing this at dconf. Essentially, we need 
>> a way to
>> create a functor similar to how C++ lambdas do. The most 
>> straightforward
>> way would involve string mixins, and you'd do something like
>>
>> auto f = makeFunctor!"function code here"(arguments);
>
> Which could look like this with AST macros ;)
>
> auto f = makeFunctor(args) {
>    function code goes here, no strings are needed
> }

Well, this kind of syntax can be implemented without AST macros. 
But there are syntactic ambiguities if the delegate has 
parameters:

auto f = makeFunctor(args) (a, b) { }

Is the second pair of parens the delegate's parameter list, or a 
call a callable object returned by makeFunctor?

Besides, do we require a semicolon after the closing brace? If 
yes, that would be at odds with the rest of the language, but if 
not, we cannot easily chain such calls...


More information about the Digitalmars-d mailing list