Java 8 lambda syntax decided - same as C# and Scala
mail.openjdk.java.net
mail.openjdk.java.net
From another point of view ... it's a shame we needed so many years to come up with "let's do it like C#".
If I understand checked exceptions correctly, removing them will only allow more programs to compile. The old ones will still be valid as their (now optional) try-catch/finally blocks won't go anywhere.
If users wanted to they could declare 'throws TheirParticularRuntime' all the way up the stack and it would work like checked exceptions, right?
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 propagate up the call stack by default if you don't do anything, making it harder for people to write bad exception handling code.
I see a lot of comments here that seem to assume catching the exception is the only option.
In the end you write boilerplate and fight the IDE.
A decent roundup of some of the arguments for and against: http://www.ibm.com/developerworks/java/library/j-jtp05254/in...
catch (FirstException | SecondException ex)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.
Frankly this is one complaint about java I have never understood. I, myself, have missed checked exceptions in C# when I have had to port Java code to C#.
Now they will just need to grapple over with what kind of scoping that Java's lambdas will have.
Do you mean about how lambdas are implemented from a bytecode standpoint, or other types of issues?
For instance, there was a long battle over whether () would be enough to invoke a lambda, or whether some sort of apply() or invoke() or .() method would be required, and a lot of the problems with () come down to differences between the way fields and methods are inherited in Java, esp. w.r.t. shadowing. I don't even know how it all came down, but it gets nasty, a lot of this stuff just wasn't designed with lambdas in mind.
try { /* for a really long time */ } finally { /* made a decision */ } Indent by four spaces. I think two spaces are enough try { /* thinking for a really long time */ } catch (TimeoutException ex) { /* copy from C# */ } 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 code and add empty finally statements to them all.Scala, on the other hand, still allows standalone try-statements. And Scala and C# also have a better closure syntax than that language, and are more worthy to be copied by Java 8.
An empty try block allows us to repeat variable declarations with the same name in a long stretch of scripty-style code.
> You can always use braces to separate a block without try at all.
You can use braces in Java, but not in that language I'm talking about: it'll throw an error saying "Ambiguous expression could be a parameterless closure expression, an isolated open code block, or it may continue a previous statement; solution: Add an explicit parameter list, e.g. {it -> ...}, or force it to be treated as an open block by giving it a label, e.g. L:{...}, and also either remove the previous newline, or add an explicit semicolon ';'".
The standalone-try looked far more elegant than:
dudlabel:{
//do something
}
Better for that despot to re-enable standalone try-statements in Groovy than petitioning the Java 8 designers that thin arrows look better than industry-standard thick arrows.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?
Example:
...
...
{ //critical section
QMutexLocker locksInConstructorAndUnlocksInDestructor(&mutex);
a = doStuff(a,b,c);
b = doOtherStuff(a,b,c);
c = andAnother(a,b,c);
}
..
..
It would be overkill to make this block a function, especially when code in critical section changes many variables.But before that stage, nested scopes are useful because I'm far less likely to pass the wrongly-named variable into a function when I'm cutting and pasting code. An example coding snippet...
def out= new File("blah")
try{
def testdata= "abcdefg"
def result= SomethingToTest(testdata, out)
assert result == result.getSth()
}
try{
def testdata= "hijk"
try{
def result= SomethingToTest(testdata, out)
assert result == result.getSth()
}
try{
def result= SomethingToTest(testdata)
assert result == result.getSth()
}
}
If I use testdata1, testdata2, result1, result2, etc, I might forget to rename a variable after cutting and pasting, and think a test works when it doesn't.Unless you're relying on the try {} to avoid bombing when there are assertion errors? That just seems a bad way of doing unit testing.
tr:{
//...
}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. 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
Are you the person who believes the Groovy developers are personally working to invalidate your web tutorials so they can discredit your fork of the language?Lest HN think I'm being ungenerous, please read http://groovy.codeplex.com/wikipage?title=Blog01 , which features similar language regarding "despots".
Of course, Groovy also differentiates itself from Guava
by having a dedicated language syntax, which is no doubt
why the cartel don't want to standardize it. If it was
standardized, they couldn't make some random blogger's
sample code stop working by suddenly requiring all
try-statements to have an empty finally clause.Probably just one of them.
Try block are for just that, trying something you know ahead of time might fail. There's no point in using the try paradigm if you don't also intend to handle the expected failure.
new Thread( => { doStuff(); }).Start();
new Thread(() => { doStuff(); }).Start();
Or can they be elided in C# or Scala? 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(new Runnable() { public void run() {doStuff();} }).start();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.
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.