Support for lazy evaluation of arguments in Preconditions
See original GitHub issueA seemingly innocent Precondition
that looks like this…
// Make sure 'user' is null
Preconditions.checkState(user == null, "User is not null. User id: %s", user.getId());
…actually throws an NPE when the precondition is met (user == null
) since user.getId()
is eagerly evaluated. The obvious workaround is to provide an argument like…
Preconditions.checkState(user == null, "User is not null. User id: %s", new Object() {
@Override
public String toString() {
return user.getId();
}
});
…but it feels clunky at best.
It would be very slick if Preconditions
would support lazy evaluation of parameters, for example by special treatment of Supplier<?>
arguments, so one could write something like
Preconditions.checkState(user == null, "User is not null. User id: %s", (Supplier<?>) () -> user.getId());
The toString
implementation of Supplier
is pretty meaningless so I don’t this would infringe on other use-cases or cause any back compat issues.
Another alternative could be to use a dedicated interface, such as LazyArgument
and have a factory method in Preconditions
to allow for something like
Preconditions.checkState(user == null, "User is not null. User id: %s", Preconditions.lazyArg(() -> user.getId()));
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
6.5 Lazy evaluation | Advanced R
In R, function arguments are lazily evaluated: they're only evaluated if accessed. ... Lazy evaluation is powered by a data structure called a...
Read more >Java static code analysis - SonarSource Rules
Lazy initialization of "static" fields should be "synchronized". Code Smell ... "Preconditions" and logging arguments should not require evaluation.
Read more >Lazy Evaluation Of Function Arguments
Lazy evaluation of function arguments dramatically extends the expressive power of functions. It enables the encapsulation into functions of many common coding ...
Read more >Scala Language Tutorial => Arguments lazy evaluation
Scala supports lazy evaluation for function arguments using notation: def func(arg: => String) . Such function argument might take regular String object or ......
Read more >logging lazy evaluation java, logging in java stream, java 8 log4j ...
Java 8 lambda support for lazy logging In release 2.4, ... "Preconditions" and logging arguments should not require evaluation (squid:S2629) Passing message ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
(We should promote flogger more.)
I try to never say never, but the lambda approach is probably unlikely to fly.
They might not be what you want, but I’ll leave this open while we think about failState & failArgument a sec.
Sorry I missed that.
We think that
is not sufficiently better code than
And in fact, anyone can read the latter code and know exactly what it does, which isn’t the case at top.
Guava has never wanted to be a different way to do things. We have always strived to add only features that provide better ways than the universal idioms. Preconditions provides value just by being usable most of the time; it doesn’t need to be “all of the time”.