Timeline for Java8: Lambda expression not expected here
Current License: CC BY-SA 3.0
10 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 22, 2017 at 20:12 | comment | added | Alexey Romanov | @Holger Lambda expressions not having values very directly contradicts docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.1, they'd need to be treated specially in the rules for assignment contexts (since they start by evaluating the expression), etc. | |
| May 22, 2017 at 20:08 | comment | added | Alexey Romanov |
@Holger "it doesn’t solve the OP’s misconception" I entirely agree. I just don't agree that this answer does, except by promoting a different misconception. "E.g. the spec does not mention return statements explicitly" This actually seems like an inconsistency: from docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.17 it looks like return creates an assignment context, despite not fitting the phrasing in that section.
|
|
| May 22, 2017 at 16:26 | comment | added | Holger | @Alexey Romanov: but anyway, if you want to see the generated object as "the value of a lambda expression", go ahead, but it doesn’t help in any way, as it doesn’t solve the OP’s misconception. Seeing a lambda expression as a function rather than a value, on the other hand, does help understanding how pointless the OP’s attempt is. | |
| May 22, 2017 at 16:23 | comment | added | Holger |
@Alexey Romanov: sorry, I was using the term "assignment context" in the broader way of all contexts where a lambda expression gets assigned to a functional interface type. E.g. the spec does not mention return statements explicitly, still returning a lambda expression when the declared return type is a functional interface, will assign the lambda expression to a functional interface type. So in all contexts, where lambda expressions are legal, they are assigned to a functional interface type.
|
|
| May 22, 2017 at 14:47 | comment | added | Alexey Romanov | @Holger No, this section applies in all 3 contexts where lambdas are legal. "it’s describing a "value" whose concatenation with a String has an unspecified, meaningless result" In this context it doesn't describe any value, but to say OP's code is illegal because lambdas don't have values is the wrong way around: this lambda doesn't have a value because this isn't a legal context for a lambda. | |
| May 22, 2017 at 14:15 | comment | added | Holger |
@Alexey Romanov: the section is only describing the "Run-Time Evaluation of Lambda Expressions" in an assignment context, but granted, that wording can indeed mislead into thinking as the instance being the value of the lambda expression and not the result of the assignment. That would require changes, if lambda expressions are supported in different contexts. Still, it’s describing a "value" whose concatenation with a String has an unspecified, meaningless result.
|
|
| May 22, 2017 at 13:57 | comment | added | Alexey Romanov | @Holger The specification disagrees (in docs.oracle.com/javase/specs/jls/se8/html/…): "The value of a lambda expression is a reference to an instance of a class with the following properties..." | |
| May 22, 2017 at 13:42 | comment | added | Holger |
@Alexey Romanov: no, lambda expression do not have values. You can convert lambda expressions to an object of an unspecified type implementing a functional interface type having a toString() implementation returning an unspecified, most likely meaningless string. Perhaps that’s what you meant. The lambda expression itself has no value. In the current Java version, it doesn’t even have a type.
|
|
| May 20, 2017 at 14:59 | comment | added | Alexey Romanov | This is wrong. In contexts where they are legal lambdas do have values. | |
| May 20, 2017 at 6:20 | history | answered | Jean-Baptiste Yunès | CC BY-SA 3.0 |