Re:The Neat Programming Language
by dataangel on Monday December 25, 2023 @04:08PM
(#38766119)
For the uninitiated what are wild about D lambdas? I'm familiar with C++ lambdas and Python lambdas.
[ Parent
| on HN ]
Re:The Neat Programming Language
by FeepingCreature on Monday December 25, 2023 @04:26PM
(#38766283)
Okay so if you're calling a D std.algorithm function, for instance
int factor = 2;
assert([2, 3].map!(a => a * factor).array == [4, 6]);
Then the `!` indicates that you're actually passing the lambda as a compiletime parameter to `map`. But the lambda can access the surrounding context! How does it pass a runtime value at compile time?So what you're passing is actually purely a function symbol. The way that it gets the stack reference to the surrounding function is that it's actually a nested function. And the way that `map` gets the stack reference to pass to the lambda is that, effectively, that instance of `map` is also a nested function of the calling function.
That's also why you cannot pass a lambda as a template parameter to a class method in D: it already has a context parameter, ie. the class reference.
In Neat, the value of the lambda is the stackframe reference, and it's just passed as a regular parameter:
int factor = 2;
assert([2, 3].map(a => a * factor).array == [4, 6]);
Which avoids this whole issue at the cost of requiring some cleverness with refcounting.This "Just pass it as a name" pattern has a been a complete disaster for D IMO. It was before my time but I think the explanation for why seems to be annoyingly along the lines of "dmd optimizer likes it".
It also encourages people not to think about what the structure of their templates, so you can end up with truly massive amounts of duplication.
Re:The Neat Programming Language
by FeepingCreature on Monday December 25, 2023 @09:13PM
(#38768350)
Well, D lambdas and Neat lambdas cash out the same at the backend level. There shouldn't be a performance difference. If you're passing the context as an explicit parameter, that should turn out exactly the same as passing it as an implicit stackframe parameter. The difference is that instead of instantiating the template with the lambda, we're instantiating it with a type that uniquely corresponds to the lambda - it's pretty similar in the end.