Java lambda only throwing expression-style instead of statement-style [duplicate]
This question already has an answer here:
Why must throw statements be enclosed with a full code block in a lambda body?
3 answers
In Java (using Java 8 currently), I can write this and all will compile nice and well:
Supplier<Long> asd = () -> {
throw new RuntimeException();
};
Yet, I cannot write this:
Supplier<Long> asd = () -> throw new RuntimeException(); // This won't compile :(
Does anyone know why Java's implementation does not allow such style (expression lambda) and only the statement/code-block style lambda?
I mean, since the lambda is only throwing the RuntimeException, why isn't the JDK able to infer the lambda expression as:
new Supplier<Long>() {
@Override
public Long get() {
throw new RuntimeException();
}
};
Is this documented somewhere in the specs/docs? Is this added only in JDK > 8?
java lambda java-8 throw functional-interface
marked as duplicate by Ridcully, Heinzi, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 30 at 15:29
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why must throw statements be enclosed with a full code block in a lambda body?
3 answers
In Java (using Java 8 currently), I can write this and all will compile nice and well:
Supplier<Long> asd = () -> {
throw new RuntimeException();
};
Yet, I cannot write this:
Supplier<Long> asd = () -> throw new RuntimeException(); // This won't compile :(
Does anyone know why Java's implementation does not allow such style (expression lambda) and only the statement/code-block style lambda?
I mean, since the lambda is only throwing the RuntimeException, why isn't the JDK able to infer the lambda expression as:
new Supplier<Long>() {
@Override
public Long get() {
throw new RuntimeException();
}
};
Is this documented somewhere in the specs/docs? Is this added only in JDK > 8?
java lambda java-8 throw functional-interface
marked as duplicate by Ridcully, Heinzi, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 30 at 15:29
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
If I am not mistaken, the{}
surrounding your statementthrow new RuntimeException()
is an "anonymous function". my guess is that the devs decided that all calls should be in a method, and not in the style you are asking for. this is in no way authoritative, hence the comment, rather than an answer...
– JoSSte
Nov 30 at 12:45
9
You can not use the expression syntax, because athrow
statement is not an expression. As simple as that.
– Holger
Nov 30 at 12:48
3
because throw is not an expression in the same way as return is not an expression perhaps?
– Joakim Danielson
Nov 30 at 12:49
1
Note: C# got throw expressions in version 7. It might be a good idea to propose such a feature for Java as well (especially if Java ever gets a null coalescing operator, because that seems to be the main use case for throw expressions in C#).
– Heinzi
Nov 30 at 15:31
add a comment |
This question already has an answer here:
Why must throw statements be enclosed with a full code block in a lambda body?
3 answers
In Java (using Java 8 currently), I can write this and all will compile nice and well:
Supplier<Long> asd = () -> {
throw new RuntimeException();
};
Yet, I cannot write this:
Supplier<Long> asd = () -> throw new RuntimeException(); // This won't compile :(
Does anyone know why Java's implementation does not allow such style (expression lambda) and only the statement/code-block style lambda?
I mean, since the lambda is only throwing the RuntimeException, why isn't the JDK able to infer the lambda expression as:
new Supplier<Long>() {
@Override
public Long get() {
throw new RuntimeException();
}
};
Is this documented somewhere in the specs/docs? Is this added only in JDK > 8?
java lambda java-8 throw functional-interface
This question already has an answer here:
Why must throw statements be enclosed with a full code block in a lambda body?
3 answers
In Java (using Java 8 currently), I can write this and all will compile nice and well:
Supplier<Long> asd = () -> {
throw new RuntimeException();
};
Yet, I cannot write this:
Supplier<Long> asd = () -> throw new RuntimeException(); // This won't compile :(
Does anyone know why Java's implementation does not allow such style (expression lambda) and only the statement/code-block style lambda?
I mean, since the lambda is only throwing the RuntimeException, why isn't the JDK able to infer the lambda expression as:
new Supplier<Long>() {
@Override
public Long get() {
throw new RuntimeException();
}
};
Is this documented somewhere in the specs/docs? Is this added only in JDK > 8?
This question already has an answer here:
Why must throw statements be enclosed with a full code block in a lambda body?
3 answers
java lambda java-8 throw functional-interface
java lambda java-8 throw functional-interface
edited Nov 30 at 14:14
Jesse de Bruijne
2,46861327
2,46861327
asked Nov 30 at 12:40
Tamir Nauman
894
894
marked as duplicate by Ridcully, Heinzi, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 30 at 15:29
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Ridcully, Heinzi, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 30 at 15:29
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
If I am not mistaken, the{}
surrounding your statementthrow new RuntimeException()
is an "anonymous function". my guess is that the devs decided that all calls should be in a method, and not in the style you are asking for. this is in no way authoritative, hence the comment, rather than an answer...
– JoSSte
Nov 30 at 12:45
9
You can not use the expression syntax, because athrow
statement is not an expression. As simple as that.
– Holger
Nov 30 at 12:48
3
because throw is not an expression in the same way as return is not an expression perhaps?
– Joakim Danielson
Nov 30 at 12:49
1
Note: C# got throw expressions in version 7. It might be a good idea to propose such a feature for Java as well (especially if Java ever gets a null coalescing operator, because that seems to be the main use case for throw expressions in C#).
– Heinzi
Nov 30 at 15:31
add a comment |
If I am not mistaken, the{}
surrounding your statementthrow new RuntimeException()
is an "anonymous function". my guess is that the devs decided that all calls should be in a method, and not in the style you are asking for. this is in no way authoritative, hence the comment, rather than an answer...
– JoSSte
Nov 30 at 12:45
9
You can not use the expression syntax, because athrow
statement is not an expression. As simple as that.
– Holger
Nov 30 at 12:48
3
because throw is not an expression in the same way as return is not an expression perhaps?
– Joakim Danielson
Nov 30 at 12:49
1
Note: C# got throw expressions in version 7. It might be a good idea to propose such a feature for Java as well (especially if Java ever gets a null coalescing operator, because that seems to be the main use case for throw expressions in C#).
– Heinzi
Nov 30 at 15:31
If I am not mistaken, the
{}
surrounding your statement throw new RuntimeException()
is an "anonymous function". my guess is that the devs decided that all calls should be in a method, and not in the style you are asking for. this is in no way authoritative, hence the comment, rather than an answer...– JoSSte
Nov 30 at 12:45
If I am not mistaken, the
{}
surrounding your statement throw new RuntimeException()
is an "anonymous function". my guess is that the devs decided that all calls should be in a method, and not in the style you are asking for. this is in no way authoritative, hence the comment, rather than an answer...– JoSSte
Nov 30 at 12:45
9
9
You can not use the expression syntax, because a
throw
statement is not an expression. As simple as that.– Holger
Nov 30 at 12:48
You can not use the expression syntax, because a
throw
statement is not an expression. As simple as that.– Holger
Nov 30 at 12:48
3
3
because throw is not an expression in the same way as return is not an expression perhaps?
– Joakim Danielson
Nov 30 at 12:49
because throw is not an expression in the same way as return is not an expression perhaps?
– Joakim Danielson
Nov 30 at 12:49
1
1
Note: C# got throw expressions in version 7. It might be a good idea to propose such a feature for Java as well (especially if Java ever gets a null coalescing operator, because that seems to be the main use case for throw expressions in C#).
– Heinzi
Nov 30 at 15:31
Note: C# got throw expressions in version 7. It might be a good idea to propose such a feature for Java as well (especially if Java ever gets a null coalescing operator, because that seems to be the main use case for throw expressions in C#).
– Heinzi
Nov 30 at 15:31
add a comment |
2 Answers
2
active
oldest
votes
From the language spec JLS 15.27.2:
A lambda body is either a single expression or a block (§14.2).
throw
is not a single expression (it's a statement); so you have to use it in a block.
add a comment |
What you wrote is an invalid lambda. There is a difference between the expression with the brackets {}
and without. See the example. The following means that 1L
is returned.
Supplier<Long> asd = () -> 1L;
which is equivalent to:
Supplier<Long> asd = () -> {
return 1L;
};
However, when you write:
Supplier<Long> asd = () -> throw new RuntimeException();
It would be translated following which is an invalid lambda:
Supplier<Long> asd = () -> {
return throw new RuntimeException(); // invalid expression in Java
};
In a nutshell, you can understand () -> 1L
as a shortcut for { return 1L; }
.
This is not entirely the case, since the following program is valid:Consumer<String> consumer = str -> System.out.println(str)
. This is not elaborated toConsumer<String> consumer = str -> { return System.out.println(str); }
(which would similarly be an invalid program). Some special rules were added to accommodate expressions of typevoid
; it's just that these same rules weren't added to accommodatethrow
statements.
– Nick
Dec 4 at 2:16
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
From the language spec JLS 15.27.2:
A lambda body is either a single expression or a block (§14.2).
throw
is not a single expression (it's a statement); so you have to use it in a block.
add a comment |
From the language spec JLS 15.27.2:
A lambda body is either a single expression or a block (§14.2).
throw
is not a single expression (it's a statement); so you have to use it in a block.
add a comment |
From the language spec JLS 15.27.2:
A lambda body is either a single expression or a block (§14.2).
throw
is not a single expression (it's a statement); so you have to use it in a block.
From the language spec JLS 15.27.2:
A lambda body is either a single expression or a block (§14.2).
throw
is not a single expression (it's a statement); so you have to use it in a block.
answered Nov 30 at 12:55
Andy Turner
80.1k879133
80.1k879133
add a comment |
add a comment |
What you wrote is an invalid lambda. There is a difference between the expression with the brackets {}
and without. See the example. The following means that 1L
is returned.
Supplier<Long> asd = () -> 1L;
which is equivalent to:
Supplier<Long> asd = () -> {
return 1L;
};
However, when you write:
Supplier<Long> asd = () -> throw new RuntimeException();
It would be translated following which is an invalid lambda:
Supplier<Long> asd = () -> {
return throw new RuntimeException(); // invalid expression in Java
};
In a nutshell, you can understand () -> 1L
as a shortcut for { return 1L; }
.
This is not entirely the case, since the following program is valid:Consumer<String> consumer = str -> System.out.println(str)
. This is not elaborated toConsumer<String> consumer = str -> { return System.out.println(str); }
(which would similarly be an invalid program). Some special rules were added to accommodate expressions of typevoid
; it's just that these same rules weren't added to accommodatethrow
statements.
– Nick
Dec 4 at 2:16
add a comment |
What you wrote is an invalid lambda. There is a difference between the expression with the brackets {}
and without. See the example. The following means that 1L
is returned.
Supplier<Long> asd = () -> 1L;
which is equivalent to:
Supplier<Long> asd = () -> {
return 1L;
};
However, when you write:
Supplier<Long> asd = () -> throw new RuntimeException();
It would be translated following which is an invalid lambda:
Supplier<Long> asd = () -> {
return throw new RuntimeException(); // invalid expression in Java
};
In a nutshell, you can understand () -> 1L
as a shortcut for { return 1L; }
.
This is not entirely the case, since the following program is valid:Consumer<String> consumer = str -> System.out.println(str)
. This is not elaborated toConsumer<String> consumer = str -> { return System.out.println(str); }
(which would similarly be an invalid program). Some special rules were added to accommodate expressions of typevoid
; it's just that these same rules weren't added to accommodatethrow
statements.
– Nick
Dec 4 at 2:16
add a comment |
What you wrote is an invalid lambda. There is a difference between the expression with the brackets {}
and without. See the example. The following means that 1L
is returned.
Supplier<Long> asd = () -> 1L;
which is equivalent to:
Supplier<Long> asd = () -> {
return 1L;
};
However, when you write:
Supplier<Long> asd = () -> throw new RuntimeException();
It would be translated following which is an invalid lambda:
Supplier<Long> asd = () -> {
return throw new RuntimeException(); // invalid expression in Java
};
In a nutshell, you can understand () -> 1L
as a shortcut for { return 1L; }
.
What you wrote is an invalid lambda. There is a difference between the expression with the brackets {}
and without. See the example. The following means that 1L
is returned.
Supplier<Long> asd = () -> 1L;
which is equivalent to:
Supplier<Long> asd = () -> {
return 1L;
};
However, when you write:
Supplier<Long> asd = () -> throw new RuntimeException();
It would be translated following which is an invalid lambda:
Supplier<Long> asd = () -> {
return throw new RuntimeException(); // invalid expression in Java
};
In a nutshell, you can understand () -> 1L
as a shortcut for { return 1L; }
.
answered Nov 30 at 12:47
Nikolas
12.8k53263
12.8k53263
This is not entirely the case, since the following program is valid:Consumer<String> consumer = str -> System.out.println(str)
. This is not elaborated toConsumer<String> consumer = str -> { return System.out.println(str); }
(which would similarly be an invalid program). Some special rules were added to accommodate expressions of typevoid
; it's just that these same rules weren't added to accommodatethrow
statements.
– Nick
Dec 4 at 2:16
add a comment |
This is not entirely the case, since the following program is valid:Consumer<String> consumer = str -> System.out.println(str)
. This is not elaborated toConsumer<String> consumer = str -> { return System.out.println(str); }
(which would similarly be an invalid program). Some special rules were added to accommodate expressions of typevoid
; it's just that these same rules weren't added to accommodatethrow
statements.
– Nick
Dec 4 at 2:16
This is not entirely the case, since the following program is valid:
Consumer<String> consumer = str -> System.out.println(str)
. This is not elaborated to Consumer<String> consumer = str -> { return System.out.println(str); }
(which would similarly be an invalid program). Some special rules were added to accommodate expressions of type void
; it's just that these same rules weren't added to accommodate throw
statements.– Nick
Dec 4 at 2:16
This is not entirely the case, since the following program is valid:
Consumer<String> consumer = str -> System.out.println(str)
. This is not elaborated to Consumer<String> consumer = str -> { return System.out.println(str); }
(which would similarly be an invalid program). Some special rules were added to accommodate expressions of type void
; it's just that these same rules weren't added to accommodate throw
statements.– Nick
Dec 4 at 2:16
add a comment |
If I am not mistaken, the
{}
surrounding your statementthrow new RuntimeException()
is an "anonymous function". my guess is that the devs decided that all calls should be in a method, and not in the style you are asking for. this is in no way authoritative, hence the comment, rather than an answer...– JoSSte
Nov 30 at 12:45
9
You can not use the expression syntax, because a
throw
statement is not an expression. As simple as that.– Holger
Nov 30 at 12:48
3
because throw is not an expression in the same way as return is not an expression perhaps?
– Joakim Danielson
Nov 30 at 12:49
1
Note: C# got throw expressions in version 7. It might be a good idea to propose such a feature for Java as well (especially if Java ever gets a null coalescing operator, because that seems to be the main use case for throw expressions in C#).
– Heinzi
Nov 30 at 15:31