Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the new lambda syntax?

Tags:

lambda

d

I've heard there is going to be a new syntax in DMD 2.058 for anonymous functions, but I can't find any information on it. What is the new syntax, and is the old syntax going to be deprecated?

like image 274
Arlen Avatar asked Jan 13 '12 22:01

Arlen


2 Answers

I believe it's like C#'s.

The following are equivalent:

delegate(i, j) { return i + j; }
(i, j) => i + j

as are:

delegate(i) { return i; }
(i) => i
i => i   // Can leave off parentheses

as are:

delegate(int i) { return i; }
(int i) => i

though I'm not sure whether explicit return types are also supported in the new syntax.

like image 119
user541686 Avatar answered Oct 16 '22 16:10

user541686


No current lambda functionality is being removed. It's just that a new, terser syntax is being introduced for those who want it. As Merhdad says, it's essentially C#'s syntax, though I don't know if it's 100% identical.

So instead of doing something like

auto found = find!((a){return func(a) == value;})(range);

you do something like

auto found = find!(a => func(a) == value)(range);
like image 8
Jonathan M Davis Avatar answered Oct 16 '22 16:10

Jonathan M Davis