Skip to content

Update signature of Map.computeIfAbsent - #156

Open
msridhar wants to merge 2 commits into
jspecify:mainfrom
msridhar:compute-if-absent
Open

Update signature of Map.computeIfAbsent#156
msridhar wants to merge 2 commits into
jspecify:mainfrom
msridhar:compute-if-absent

Conversation

@msridhar

@msridhar msridhar commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

mappingFunction is allowed to return null to avoid updating the mapping, and in this case, computeIfAbsent returns null. Also updated overrides in @NullMarked classes.

I added the @PolyNull comment on Map.computeIfAbsent since I think we'd use it here if it were available; didn't add the comment on all the overrides.

Discovered in ben-manes/caffeine#2004 (comment)

@msridhar
msridhar requested review from cpovirk and wmdietl August 22, 2026 21:04
@cpovirk

cpovirk commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

That is all true, yet this has been the only(?) case in which I've been holding out hope that we can declare the signature as more restrictive than it really "should" be, just because a @Nullable return type is so inconvenient for 99% of callers. There is some discussion in #102, but beware that the actual title of that issue is about going even further in the opposite direction (as probably no one actually wants), even though the text goes on to mostly discuss the thing proposed here that people reasonably would want :) I'd be interested in your read on it, whether here or there. I do kind of feel like we'll need to go with the "honest" type eventually, though it would be nice to sort out the Kotlin story first, and I haven't found time :(

@msridhar

Copy link
Copy Markdown
Collaborator Author

Ah...bummer. We could change the type of mappingFunction to be Function<? super K, ? extends @Nullable V>, but (inconsistently) leave the return type as V (without a @Nullable). But the current version is safer.

We have an open issue (uber/NullAway#1616) on adding some kind of ad hoc @PolyNull support to NullAway for library models, for orElseGet. We might have to do the same thing here, or just ask users to suppress the warning. I'll have to think about how best to recommend a narrow suppression, though, rather than the entire surrounding method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants