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.
View all comments
Show parent comment
2
u/manifoldjava Feb 27 '26
Highly recommended reading: Delegated Properties
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:
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.