On Thu, 3 Apr 2025 14:46:49 GMT, Maurizio Cimadamore <[email protected]>
wrote:
>> Per Minborg has updated the pull request incrementally with one additional
>> commit since the last revision:
>>
>> Make the sqrt example different
>
> src/java.base/share/classes/java/lang/StableValue.java line 211:
>
>> 209: * StableValue.function(CACHED_KEYS, LOG2_ORIGINAL);
>> 210: *
>> 211: * public static double log2(int a) {
>
> IMHO this example reveals a lack of compositionality of the partial functions
> we have. If the functions returned optional (to indicate "not defined"), then
> code like this would become much simpler to express:
>
>
> return LOG2_CACHED.apply(a)
> .orElse(LOG2_ORIGINAL.apply(a));
>
>
> It's not just that this is more fluent -- this is also using the caching
> function to check whether the input is cached or not -- instead of having a
> separate check (potentially duplicating the costs).
I agree with this observation. We should look at providing true partial
functions using for example `Optional`.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/23972#discussion_r2028394873