i notice that, when subclassing `MutableMap`, over...
# stdlib
b
i notice that, when subclassing
MutableMap
, overriding
compute
gives the signature
override fun compute(key: T, remappingFunction: BiFunction<in T, in U?, out U?>): U?
, but overriding
computeIfAbsent
gives
override fun computeIfAbsent(key: T, mappingFunction: Function<in T, out U>): U
(note the missing
?
after
U
on the return type) but
computeIfAbsent
is documented as being able to return null. is this a bug?
computeIfPresent
also correctly uses
U?
as the return type.
@uli true, but i wondered why the others explicitly used
U?
and i was running into an issue where, even though i had defined
U
as
U : Any?
in the class, explicitly returning
null
from that function was giving me an error
u
The others are explicitly nullable because they can return null, even when U is non nullable. You can not make nullability go away for them
b
hmm, i got the impression
computeIfAbsent
was the same from the comment about the return value:
@return the current (existing or computed) value associated with the specified key, or null if the computed value is null
u
The others return a value U if the key is find, null otherwise. ComputeIfAbsent returns a value U if the key is fond and a computed value U of not.
That's the difference
The docs just point out that the computed value U could be null if U happens to be a nullable type
b
gotcha...that's a subtle one. thanks @uli!
u
Any time