<div dir="ltr"><div>Hi all,</div><div><br></div><div>(I'm new around here, so please excuse me for this late comment or in case this was already discussed in some place I missed; I have seached Jira and the mailing-list via search engine unsuccessfully).</div><div><br></div><div>I just discovered the StableValue proposal of JEP-502, that's a preview feature for Java 25. I really welcome such change as -like most Java developers- I've had to repeat some boilerplate code to create cached values.</div><div>However, I have the impression that StableValue is a bit redundant with Optional, and that maybe another approach based on Optional could work.</div><div><br></div><div>Concretely, I'm thinking of just adding some static constructor to Optional that would be like `Optional<T> Optional.supplyNullable(Supplier<T> supplier)`, one would be able to use it in such way:</div><div>```</div><div>Optional.supplyNullable(() -> {<br>  String res;<br>  ...compute res...<br>  return res;<br>})</div><div>```</div><div>and for more complex cases</div><div>```</div><div>Optional.supplyNullable(new Supplier<String>() {</div><div>  private int state = 0;</div><div>  @Override</div><div>  String get() {</div><div>    return stateful() + stateful() + stateless();</div><div>  }</div><div><br></div><div>  private String part1() {</div><div>    return Integer.toString(state++);</div><div>  }</div><div><div>  private String part2() {</div><div>    return System.getProperty("jvm.vendor");</div><div>  }</div></div><div>})</div><div>```</div><div><br></div><div><br></div><div>and that would leave the ability to fully encapsulate/isolate the field computation in the provided supplier (just like the StableValue) and that would provide at least the immutability goal of StableValue. I'm not much aware of the Optional internals to know whether it would fit other requeirements (I actually didn't spot any other requirements on the JEP)</div><div>So I'm just sending that here so that this `Optional.supplyNullable(,,,)` approach gets considered as an alternative. I honestly don't know whether it'd be better, I just have a strong feeling that it can be worth having it evaluated as an alternative in the JEP before things get final.</div><div><br></div><div>Cheers,</div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>Mickael Istria<br></div><a href="https://www.eclipse.org/eclipseide" target="_blank">Eclipse IDE</a> developer, for <a href="https://developers.redhat.com/" target="_blank">Red Hat</a></div></div></div></div></div></div></div>