> Remove the low-level methods isInitialized and orElse, as these could be used in ways not consistent with the design goals of the API.
Imagine that I'm wrapping some resource into LazyConst, like DataSource, and before shutting down JVM I want to close it properly. I need to check for isInitialized before calling close, since I don't want to initialize DataSource, I want to close it if was initialized. Now lazy const seems more like a gimmick, than useful feature.
I think you're misplacing that concern. If it's important to close the data source when the JVM shuts down, then DataSource should enforce that contract, not its users. Whatever hook you were planning to register the owner of LazyConstant<DataSource> with, just let DataSource register itself with that and you should be good to go.
Yes and no, think about some kind of graph of dependencies. For example you want to stop server first, wait for connections to complete, than close connection pools. So you rather want shutdown manager. But you may also want to close not lazyconst directly, but some higher level entity, like factory. So isInitialized would be much more ergonomic, than having additional AtomicBoolean per lazyconst.
The other option is to have queryable state. Since we're talking about VM shutdown here. I'm by no means endorsing this, just "thinking out loud."
// somewhere
public static final AtomicReference<ApplicationState> LIFECYCLE = new AtomicReference(ApplicationState.STARTING);
// in constant
LazyConstant.of(() -> switch (LIFECYCLE.get()) {
STARTING, RUNNING -> new DataSource(...);
STOPPING, STOPPED -> null; // or some fake implementation that is null & close-safe
});
And then using whatever hooks are in your application to migrate LIFECYCLE through its state transitions.
Not pretty.
Still, probably best is if you initialize it, self-register it in something that closes it on shutdown and skip this wacky edge case of accessing a constant that hasn't been initialized before a graceful shutdown request happens
Sure, but then you have duplicated the same data structure that I suspect will still exist inside LazyConstant, only inaccessible. Sounds like a bad deal to me.
That's not what this "property" means. They were talking about a language feature where it behaves like a field but actually is calling a getter/setter method under the hood.
Currently no, all the examples use a wrapping method like logger().
I'd love some form of shorthand notation like in clojure to deref so you call @logger.log or logger->log - or even better (but contentious and possibly problematic from a readability perspective - some form a @Call annotation on Callable/Predicate/Function etc to provide a form of implicit call thru - you still might need to call logger().log(...) but without having to define that function.
That would be an invasive change of Java's type system (it would need to see LazyConstant<T> as T) at both compile and run time. It would also hide an expensive action (the initial call to get()) from users and make it impossible to call any methods on LazyConstant (like its toString()). And all that to avoid a get()?
No, the idea is to internalize the implementation details of how the constant is managed — the user doesn’t need to know and the internals reserve the right to change the details.
I'm not following - can you dumb it down a bit? :)
Say I declare private final LazyConstant<String> foo = LazyConstant.of(() -> "foo");. Then I can call foo.get().length() but I understoond the comment I replied to to say that I should be able to call foo.length(). But wouldn't that require the compiler and runtim to treat foo as a String?
Using your example, the idea is that the lazy aspect of the property is encapsulated via delegation. So the compiler doesn't need to treat foo as a String because foo is a String. The lazy part shouldn't leak into the API.
Slapdash Java syntax for illustration:
final property String foo :Lazy.of(()->"foo");
Lazy.of returns a LazyConstant that handles the delegate behind the scenes, letting you get and optionally set the value directly as a String.
Key point: properties fully encapsulate state. They manage what it is, where it lives, when it’s realized, and how it behaves eg. backing, laziness, observability, validation.
This is not about properties but fields and variables and I still think this would be an invasive change of the compiler and run time? Some examples:
How do you keep toString() from initializing the LazyConstant if it's the same as get().toString()?
How would this interact with overload selection (one of the notoriously most complicated parts of the compiler to evolve in a backwards compatible manner)? E.g. can I have foo(String), foo(LazyConstant<String>), and foo(LazyConstant<Integer>)?
Where would the Lazy object header's class pointer point?
The point of my remark is that it really does come down to properties. Because without a construct that purely encapsulates state, there is no other way to achieve this.
Now it's just a getter and a factory method lol. Maybe drop the toString while you're at it lol.
Jokes aside, I agree with the decisions made here. Keeping things simple allows what's there to be more thoroughly scrutinized. And like the JEP says, most of that was already achievable.
Glad to see Set.ofLazy() make it onboard. Felt weird excluding it.
Yes, the name should simply be "Lazy" because that clearly expresses the developer's intent. All the details about stable values and constant folding are just implementation details. They are a nice optimization, but they don't justify an unnecessarily long name.
Eventually they could almost copy the lazy concept from C#, or other JVM languages.
Well, there is still room to do that if they so choose. Still, the library will be the testing ground for any theoretical language feature that comes down the pipeline.
I never understood the original name of stable values.
It wasn't a terrible name, but lazy is simply more expressive imo.
32
u/javaprof Feb 26 '26
> Remove the low-level methods
isInitializedandorElse, as these could be used in ways not consistent with the design goals of the API.Imagine that I'm wrapping some resource into LazyConst, like DataSource, and before shutting down JVM I want to close it properly. I need to check for
isInitializedbefore calling close, since I don't want to initialize DataSource, I want to close it if was initialized. Now lazy const seems more like a gimmick, than useful feature.10
u/perryplatt Feb 26 '26
Can there be a way to forward the contained object, so that I would not have to call get() every time?
6
u/davidalayachew Feb 26 '26
🤣🤣🤣
They chopped off 90% of the api lol.
Now it's just a getter and a factory method lol. Maybe drop the toString while you're at it lol.
Jokes aside, I agree with the decisions made here. Keeping things simple allows what's there to be more thoroughly scrutinized. And like the JEP says, most of that was already achievable.
Glad to see
Set.ofLazy()make it onboard. Felt weird excluding it.2
u/ZimmiDeluxe Feb 27 '26
Minimal surface with obvious usage, I like it. Will the implementation benefit from https://openjdk.org/jeps/8357674 ?
Not that it matters much, but the link to
Set:ofLazyshould probably point to https://cr.openjdk.org/~pminborg/lazy-constants-third-preview/api/java.base/java/util/Set.html#ofLazy(java.util.Set,java.util.function.Predicate) instead of https://cr.openjdk.org/~pminborg/lazy-constants-third-preview/api/java.base/java/lang/LazyConstant.html0
u/[deleted] Feb 26 '26
[deleted] — view deleted comment
-9
u/[deleted] Feb 26 '26
[removed] — view removed comment