You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Notice how mutable.Map and mutable.LongMap produce different outputs when getOrElseUpdate is used. When not used, it is replaced with an implementation similar to what's in MapOps (I use contains instead of get because I ignore values, but the behavior is the same with get).
I believe this is a bug because LongMap#getOrElseUpdate should behave as the default MapOps implementation and because LongMap[Unit] should behave like Map[Long,Unit] (in a single-threaded context).
One could argue that getOrElseUpdate makes no such guarantees when the by-value code modifies the map, but:
the documentation is silent about that;
it's not an unreasonable scenario (my actual application is a memoized recursive function, without randomness);
there is a comment inside the LongMap#getOrElseUpdate source that says:
// It is possible that the default value computation was side-effecting
// Our hash table may have resized or even contain what we want now
// (but if it does, we'll replace it)
which suggests that an effort was made to handle this situation.
I'm using Scala 3.5.2 (which seems to be using the 2.13.14 collections) in my experiments.
The text was updated successfully, but these errors were encountered:
When Scala 3 was new, there was a discussion somewhere that 3-indent would be the norm (maybe it wasn't serious). I adopted it (including all the code examples in a book) and I'm very happy with it. Four is too much for me, and 2 (which I use in Java) not enough when omitting braces. BTW, it looks like ADA style guides recommend using a 3-indent... 😉
Here's a self-contained example that shows the incorrect behavior:
(I use Scala 3 syntax but this is a Scala 2 library issue).
The output is:
Notice how
mutable.Map
andmutable.LongMap
produce different outputs whengetOrElseUpdate
is used. When not used, it is replaced with an implementation similar to what's inMapOps
(I usecontains
instead ofget
because I ignore values, but the behavior is the same withget
).I believe this is a bug because
LongMap#getOrElseUpdate
should behave as the defaultMapOps
implementation and becauseLongMap[Unit]
should behave likeMap[Long,Unit]
(in a single-threaded context).One could argue that
getOrElseUpdate
makes no such guarantees when the by-value code modifies the map, but:LongMap#getOrElseUpdate
source that says:I'm using Scala 3.5.2 (which seems to be using the 2.13.14 collections) in my experiments.
The text was updated successfully, but these errors were encountered: