5 ms·
Lambdas are a lot of fun on C#. new Thread( => { doStuff(); }).Start();
by MatthewPhillips 15y ago
Lambdas are a lot of fun on C#.
new Thread( => { doStuff(); }).Start();
- onedognight 15y agoAccording to the grammar int the OP, you need ()'s, no? new Thread(() => { doStuff(); }).Start(); Or can they be elided in C# or Scala?
- gnuvince 15y agoThe empty () are required in Scala. Don't know about C#.
- latch 15y agoyes, they are in C# as well.
- swax 15y agoActually for just that example you don't need any of it. new Thread(doStuff).Start();
- MatthewPhillips 15y agonah, it would have to be new Thread(new ThreadStart(doStuff)).Start();
- jeffz 15y agoSorry, but since C# 2.0 we could just write "new Thread(DoStruff)" or "SomeDelegate del = SomeMethod".
- politician 15y agoI 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");
- linuxhansl 15y agoBig deal: new Thread(new Runnable() { public void run() {doStuff();} }).start();
- ppog 15y agoBut 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.
- cbs 15y agoit'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.
- deleted 15y ago[deleted]
- moomin 15y agoCool. 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.