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 a lambda expression be determined?
4 Answers
Well, a lambda expression is just an instance of a special anonymous class that only has one method. Anonymous classes can "capture" variables that are in the surrounding scope. If your definition of a stateful class is one that carries mutable stuff in its fields (otherwise it's pretty much just a constant), then you're in luck, because that's how capture seems to be implemented. Here is a little experiment :
import java.lang.reflect.Field;
import java.util.function.Function;
public class Test {
public static void main(String[] args) {
final StringBuilder captured = new StringBuilder("foo");
final String inlined = "bar";
Function<String, String> lambda = x -> {
captured.append(x);
captured.append(inlined);
return captured.toString();
};
for (Field field : lambda.getClass().getDeclaredFields())
System.out.println(field);
}
}
The output looks something like this :
private final java.lang.StringBuilder Test$$Lambda$1/424058530.arg$1
The StringBuilder
reference got turned into a field of the anonymous lambda class (and the final String inlined
constant was inlined for efficiency, but that's beside the point). So this function should do in most cases :
public static boolean hasState(Function<?,?> lambda) {
return lambda.getClass().getDeclaredFields().length > 0;
}
EDIT : as pointed out by @Federico this is implementation-specific behavior and might not work on some exotic environments or future versions of the Oracle / OpenJDK JVM.
-
6I'm sorry, but I don't believe that having or not having fields is a sufficient condition in making a method implementation (a lambda) stateless nor statefull. I'm pretty sure
() -> ThreadLocalRandom.current().nextInt()
cannot be considered 'stateless' but your test won't show any captured fields. Feb 20, 2018 at 15:27 -
1@JeremyGrand that's exact, but I have made my understanding of "stateful" pretty clear by restricting it to local data. Global side-effects are out of the scope of my answer and would require thorough (and most likely hopeless) interpretation of every single line of code.– ttznFeb 20, 2018 at 15:34
-
5If you define the lambda in a non-static context, you also get a reference to the enclosing
this
instance... All this is carefully and deliberately undocumented, and cannot be trusted in any way. It is explicitly said that all this stuff is implementation-specific, meaning that no useful code can be written that uses anything of this.– fpsFeb 20, 2018 at 18:33 -
2@FedericoPeraltaSchaffner you don’t “get” a reference to the enclosing
this
instance. A lambda expression may capture the enclosingthis
instance, if it uses it, either explicitly or by using its instance fields. A lambda expression which does not use it, does not capturethis
. This has been acknowledged by the language designers, however, they forgot to state it explicitly in the specification.– HolgerFeb 21, 2018 at 8:31 -
3@CoffeeNinja there seem to be some confusion between what a lambda expression could use and what it does use. Since your code only evaluates what the instance does use, it’s fair to mention that it does not notice whether the lambda expression does use things like
ThreadLocalRandom
orstatic
variables. And by the way, it doesn’t detect whether it “carries mutable stuff in its fields”, as all these fields arefinal
and you would have to evaluate the referenced objects, if any, to find out whether they are mutable.– HolgerFeb 21, 2018 at 8:34
No, it is not generally possible. The suggested approach of checking whether the lambda belongs to a class with a field is the next best thing, but having a field does not equal having a state.
class Stateless {
int result = 0;
public int getResult() { return result; }
}
It is possible to prove statefulness by finding two input sequence for which a given input combination returns a different result. However, it is not possible to prove that such a input sequence does not exist (any input sequence might produce a different result if prepended by another invocation).
(Even if you check the values of fields found via reflection, those might change without influencing the lambda's result, therefore not really making it stateful).
Here's a short compilable example showing both false positive and negatives, disproving the notion:
public class StatefulLambda {
static AtomicInteger counter = new AtomicInteger();
public static void main(String[] args) {
// false negative: will return different result each call
System.out.println(hasState(i -> counter.incrementAndGet()));
// false positive: will always return the same result
Object object = new Object() {
final int i = 0;
};
System.out.println(hasState(i -> object.toString()));
}
private static boolean hasState(Function<?,?> lambda) {
return lambda.getClass().getDeclaredFields().length > 0;
}
}
-
You are right: lambda can be stateless with fields and statful without fields. But it seems like it's the best we can do.– lexicoreFeb 20, 2018 at 15:54
Here's a simple and stupid idea. Just check if your lambda has fields.
For instance, consider the following stateful lambda.
List<Integer> serialStorage = new ArrayList<>();
Function<? super Integer, ? extends Integer> statefulLambda =
e -> { serialStorage.add(e); return e; };
This statefulLambda
has a private final internal field arg$1
which obviously references serialStorage
. So
statefulLambda.getClass().getDeclaredFields().length > 0
could be used as indicator that lambda is stateful.
However I have no idea if this will generally work.
-
This probably works in most cases (i have tested it with an online compiler), but it has false positives with array access, and true negatives with static field access– FerrybigFeb 20, 2018 at 21:20
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.
-
2I completely disagree with this answer. Consider a related problem: a sort algorithm takes a comparator; the comparator is required to impose a total order. There is no exception if a non-totally-ordering comparator is passed in, but it is possible to check to see if a total order is imposed! You simply call the comparator with every possible pair of items in the set being sorted, record the results in a table, and throw for any pair that violates asymmetry, reflexivity or transitivity. Real implementations do not do this even though it is possible. Feb 20, 2018 at 23:04
-
1We cannot reason that "because the implementation team chose to not implement feature X, then X must be impossible". Implementation teams make all kinds of decisions for all kinds of reasons. Feb 20, 2018 at 23:04
-
I disagree with both counter arguments. The comparator argument holds true for a comparator on Booleans; but for larger data types, the described algorithm would not work. For a 8 byte Long type you would have to compare 3.4e38 pairs, which is not achievable in a reasonable amount of time. Real implementations do not do this kind of checks because it is practical impossible.– wernerFeb 21, 2018 at 21:10
-
The second argument would hold true if it was about the kind of code that I write every day. But the Lambda Api has been one of the major changes in Java 8, the specification and community discussions lasted several years. It would have been considered as a major design flaw if a possibility to detect the statefulness of a lambda existed and it would not have been exploited. I trust that the Java community would not have accepted such a flaw. Of course, my reasoning is not - and never was - a strict proof in a rigorous mathematical sense.– wernerFeb 21, 2018 at 21:10
-
1The described algorithm does work and has been practically implemented. Do you see how? (Hint: your assumption that the entire space of possible inputs to the comparator must be validated is wrong. What is the actual space of inputs that must be validated?) Regardless of my specific example though: there are plenty of situations where an implementation team makes implementation choices to not implement a particular feature; we cannot conclude from those choices that the feature is impossible for someone else to implement. Feb 21, 2018 at 21:56
const
objects, where you can only call methods that don’t modify the object. And you’re right, this has nothing to do with whether the lambda expression captures values.