Determine if a lambda expression is stateless or stateful in Java

后端 未结 4 993
南旧
南旧 2021-02-01 20:01

Is there a function which accepts a reference to a lambda expression and returns a boolean saying whether the lambda expression is stateless or not? How can the statefulness of

4条回答
  •  既然无缘
    2021-02-01 20:15

    I would argue that it is not possible to write a function that can determine if a lambda is stateless or not:

    Looking for example at the filter method of the Stream API, the javadoc states that the parameter must be "a [...] stateless predicate" and also links to the API's definition of stateless.

    If there was a way to determine if the parameter of the filter (or any other) method was stateless or not, the Stream class would have included the possibility to throw an IllegalArgumentException in case the parameter was a stateful lambda. As this has not been implemented and only a warning was added to the javadocs, one can conclude that there is no way write a function that can determine if a lambda lambda is stateless.


    Edit (after reading the comments from Eric): There are plenty of situations where an implementation team makes implementation choices to not implement a particular feature; we usually cannot conclude from those choices that the feature is impossible for someone else to implement. In this special case, I believe its implausible that the Java 8 designers would not have found or done it if there was a (computational cheap) solution.

提交回复
热议问题