Re:Java 8 lambda syntax decided - same as C# and Scala
by linuxhansl on Thursday September 08, 2011 @10:25PM
(#2976515)
Big deal:
new Thread(new Runnable() { public void run() {doStuff();} }).start();
Re:Java 8 lambda syntax decided - same as C# and Scala
by ppog on Thursday September 08, 2011 @11:08PM
(#2976618)
But for a small function (which a lambda typically is), the syntactic overhead of "new Runnable() { public void run()" swamps the actual logic. When I read this version, it's hard for my eyes to find what work is actually being done because there are so many braces and brackets and words. The big deal of lambda syntax is that it lets you focus on the code that does the work (the doStuff) instead of the adapter (the "new Runnable() { public void run()") that the library needs around that code.
Re:Java 8 lambda syntax decided - same as C# and Scala
by cbs on Friday September 09, 2011 @09:42AM
(#2978046)
it's hard for my eyes to find what work is actually being done
Aside from if a lambda expression or anonymous class is the better choice overall, this just happens to be a case that you're just not familiar with the syntax. Its been years since I've done any work in java, but I read this over some coffee and didn't even slow down for it.
Lambdas will be nice for the situations where there is more boilerplate than implementation, but the anonymous inner class in java isn't going to die.
Re:Java 8 lambda syntax decided - same as C# and Scala
by moomin on Friday September 09, 2011 @09:34AM
(#2978022)
Cool. Now how about
companies .Where(c => c.Name.StartsWith("A")) .GroupBy(c => c.Sector, c => c.ProfitAndLoss) .Select(g => new {Sector = g.Key, Profitability = g.Sum()}
Closures are massively more compact than running around with inner classes for everything.
Re:Java 8 lambda syntax decided - same as C# and Scala
by onedognight on Thursday September 08, 2011 @09:02PM
(#2976344)
According to the grammar int the OP, you need ()'s, no?
new Thread(() => { doStuff(); }).Start();
Or can they be elided in C# or Scala?
Re:Java 8 lambda syntax decided - same as C# and Scala
by swax on Thursday September 08, 2011 @09:20PM
(#2976376)
Actually for just that example you don't need any of it.
new Thread(doStuff).Start();
Re:Java 8 lambda syntax decided - same as C# and Scala
by MatthewPhillips on Thursday September 08, 2011 @09:23PM
(#2976383)
nah, it would have to be new Thread(new ThreadStart(doStuff)).Start();
Re:Java 8 lambda syntax decided - same as C# and Scala
by jeffz on Thursday September 08, 2011 @09:56PM
(#2976456)
Sorry, but since C# 2.0 we could just write "new Thread(DoStruff)" or "SomeDelegate del = SomeMethod".
Re:Java 8 lambda syntax decided - same as C# and Scala
by politician on Thursday September 08, 2011 @11:32PM
(#2976660)
I can't resist, so,
public static class ActionExtensions
{
public static void InvokeAsync(this Action<object> target, object context)
{
new Thread(obj => target(obj)).Start(context);
}
}
((Action<object>) (Console.WriteLine)).InvokeAsync("Hello, World");
new Thread( => { doStuff(); }).Start();