Live data from Hacker News

Java 8 lambda syntax decided - same as C# and Scala

mail.openjdk.java.net

71–80 of 81 posts

Re: Java 8 lambda syntax decided - same as C# and Scala

#71
post #55

Earlier quoted context omitted.

It often leads people to write code like this: try { // do stuff } catch (TheCheckedException ex) { // ignore it } then the code continues even though it could be in a bad state if an exception did occur. It's a much better solution to let it propagate up the call stack until it can be dealt with appropriately (e.g. by ending the application, giving a 503 error on a web page request, etc.) Unchecked exceptions will p…

So add a 'throws' clause to the method in which you call the code that throws the checked exception and it will propagate upward.

Yes, this! The most irritating debugging I've had to do in Java code is when someone catches an exception, but fails to handle it effectively, resulting in another exception being thrown later. If you don't know how to handle it effectively, just add the 'throws' clause. It's less typing than try/catch, so I don't understand why people choose to make extra work for themselves and for me.

I see a lot of comments here that seem to assume catching the exception is the only option.

Re: Java 8 lambda syntax decided - same as C# and Scala

#72

Lambdas are a lot of fun on C#. new Thread( => { doStuff(); }).Start();

Big deal: new Thread(new Runnable() { public void run() {doStuff();} }).start();

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

#73
post #33

Earlier quoted context omitted.

Big deal: new Thread(new Runnable() { public void run() {doStuff();} }).start();

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 "…

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

#75
post #71
post #55

Earlier quoted context omitted.

So add a 'throws' clause to the method in which you call the code that throws the checked exception and it will propagate upward.

Yes, this! The most irritating debugging I've had to do in Java code is when someone catches an exception, but fails to handle it effectively, resulting in another exception being thrown later. If you don't know how to handle it effectively, just add the 'throws' clause. It's less typing than try/catch, so I don't understand why people choose to make extra work for themselves and for me. I see a lot of comments here…

Exactly. In Eclipse it's so ridiculously easy to make the method pass it on by adding the throw to your method. I find checked exceptions really useful every day :)

Re: Java 8 lambda syntax decided - same as C# and Scala

#76
post #36

Earlier quoted context omitted.

The single abstract method interface type of a lambda will be inferred by its declaration site.

Will they be full closures and remember the state of objects in scope?

If they will be based on anonymous inner classes, then yes, they'll capture the variables from the surrounding scope. Those variables however will be 'final', which means that you can't re-assign them, even if their values are still liable to side-effects (e.g. setting fields, mutating collections, etc).

Re: Java 8 lambda syntax decided - same as C# and Scala

#77
post #50

Earlier quoted context omitted.

> An empty try block allows us to repeat variable declarations with the same name in a long stretch of scripty-style code. Either I'm missing something here, or this is really as bad as it sounds. I understand you want nested scopes that end before the method's scope ends, but cannot for the life of me think of a defensible code example. Why not just make them into methods?

I did say scripty code. My typical scripting session from scratch starts with opening a simple editor and starting to type in code. The structure is loose, perhaps it's testing something I've written in another file in a statically-typed language. As the code evolves, I slowly give it more structure by reworking it. At some stage I MIGHT put some code into methods. But before that stage, nested scopes are useful beca…

It sounds like you want support for a bad practice. Each of those code blocks are logically distinct tasks, and it would serve readability to factor them into functions. Further, you're abusing a language construct to do something other than its purpose.

I've never programming in Scala, so I'm going to write Python code that represents what I think is a better way of doing that kind of testing:

  def test1(data):
    result = SomethingToTest(data)
    assert result == result.getSth()

  def test2(data, out):
    result = SomethingToTest(data, out)
    assert result == result.getSth()

  out = open("blah")
  test2(out, "abcdefg")
  test2(out, "hijk")
  test1("hijk")
The problem is not that copy-pasted code may be incorrect. The problem is that you're copying and pasting code and expecting it to be correct. The way you're nesting scopes is confusing.

Re: Java 8 lambda syntax decided - same as C# and Scala

#78
post #67
post #62

Earlier quoted context omitted.

I think checked exceptions are just ahead of their time. They make sense when you statically check your code against contracts, which is why they showed up again in the spec# research project. http://en.wikipedia.org/wiki/Spec_Sharp

Optional checked exceptions have some benefits. Java got them the wrong way around - unchecked should have been the default, with checked available when you need them. Because Java does them the other way around, you either get code bloat from the large numbers of exceptions you are catching and handling subtly differently or you have a generic catch(Exception e){..} block. Neither are ideal.

exactly right. I'm always surprised more people don't land here when this debate comes up.

Re: Java 8 lambda syntax decided - same as C# and Scala

#79
post #69
post #30

Earlier quoted context omitted.

There's a JVM language out there that initially allowed try-statements without catch or finally clauses, e.g try{ //for a short time only to decide } But one of its despots, five years after the language was first released, changed the syntax to make a catch or finally clause compulsory, e.g try{ //for a short time to decide }finally{} Because I had used these standalone try-statements a lot, I had to go thru all my…

Perhaps you can educate me on your better reasoning, but the only purpose I can see for an empty try block is to limit the scope of variables in a method. Quite frankly, if you find yourself in need of artificially limiting the scope of variables in a method, you've probably done too much in the method and need to refactor. Try block are for just that, trying something you know ahead of time might fail. There's no po…

If you just want to limit the scope you can just enclose your code in braces without the try. A try without the catch or finally is meaningless.

Re: Java 8 lambda syntax decided - same as C# and Scala

#80
post #36

Earlier quoted context omitted.

Will they be full closures and remember the state of objects in scope?

If they will be based on anonymous inner classes, then yes, they'll capture the variables from the surrounding scope. Those variables however will be 'final', which means that you can't re-assign them, even if their values are still liable to side-effects (e.g. setting fields, mutating collections, etc).

They are not based on anonymous inner classes (the implementation is likely to be much different) but they have the same restrictions as BDFFL_Xenu described.
Post reply on HN