-
Notifications
You must be signed in to change notification settings - Fork 10.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for lazy evaluation of arguments in Preconditions #5927
Comments
For context, Flogger (another project of ours) has this. But it has to, because logging can be conditional on factors the user can't even see. But here, it's just not enough better than regular Java:
We once considered exposing
I don't remember what happened; but it's likely we might not have been convinced this use case comes up frequently. |
Nice. I didn't know about Flogger. Personally I usually put something like...
...in a utility class somewhere to keep the call sites clean. I guess that is similar to what I don't want to drag out on something like this. When you say "it's just not enough better than regular Java", should that be interpreted as, "does not carry its weight, the team has decided to not move forward"? If yes, feel free to close as Won't fix, if no, what would the next step be? Should I file a PR? |
(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. |
I recall discussion of methods to be used like |
You're right, defeating the compiler's knowledge of abrupt termination is no bueno. But So I think this approach probably won't fly either. |
Hi @kevinb9n, Is it possible to elaborate on this a little.
We have just hit the same issue too while trying to convert all our
The workaround with the So I'm considering writing our own |
Sorry I missed that. We think that checkState(user == null, "User is not null. User id: %s", lazy(() -> user.getId())); is not sufficiently better code than if (user != null) {
throw new IllegalStateException("User is not null. User id: " + user.getId());
} 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". |
A seemingly innocent
Precondition
that looks like this......actually throws an NPE when the precondition is met (
user == null
) sinceuser.getId()
is eagerly evaluated. The obvious workaround is to provide an argument like......but it feels clunky at best.
It would be very slick if
Preconditions
would support lazy evaluation of parameters, for example by special treatment ofSupplier<?>
arguments, so one could write something likeThe
toString
implementation ofSupplier
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 inPreconditions
to allow for something likeThe text was updated successfully, but these errors were encountered: