Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Various Map implementations ported from Scala 2.12 #334

Merged
merged 1 commit into from
Jan 10, 2018
Merged

Conversation

szeiger
Copy link
Contributor

@szeiger szeiger commented Jan 5, 2018

This is another big chunk of my scala-integration branch that contains some of the more exotic Map types from Scala 2.12. They are all used in the compiler codebase.

@julienrf
Copy link
Contributor

julienrf commented Jan 8, 2018

I can see the usefulness of these collections, but couldn’t they stay in the compiler? Or is it possible to publish them in a separate artifact?

@szeiger
Copy link
Contributor Author

szeiger commented Jan 8, 2018

They are part of the standard library now. If we don't want to break stuff unnecessarily they should stay where they are.

@julienrf
Copy link
Contributor

julienrf commented Jan 8, 2018

We have already moved several parts of the standard library into separate modules. We could do the same here.

@szeiger
Copy link
Contributor Author

szeiger commented Jan 8, 2018

I made some changes for Dotty source compatibility, now it fails with a compiler error on Dotty:

[info] exception occurred while compiling /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/ArrayOps.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/BitSet.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/BufferedIterator.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/BuildFrom.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/DefaultMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/Factory.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/Hashing.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/IndexedSeq.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/Iterable.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/IterableOnce.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/Iterator.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/JavaConverters.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/LazyZipOps.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/LinearSeq.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/Map.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/Seq.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/Set.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/SortedMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/SortedOps.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/SortedSet.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/StrictOptimizedIterableOps.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/StrictOptimizedSeqOps.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/StrictOptimizedSortedSetOps.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/StringOps.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/Traversable.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/View.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/WithFilter.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/concurrent/Map.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/concurrent/TrieMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/convert/AsJavaConverters.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/convert/AsScalaConverters.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/convert/DecorateAsJava.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/convert/DecorateAsScala.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/convert/Decorators.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/convert/WrapAsJava.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/convert/WrapAsScala.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/convert/Wrappers.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/generic/BitOperations.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/generic/IsIterableLike.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/BitSet.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/HashMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/HashSet.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/ImmutableArray.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/IntMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/Iterable.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/LazyList.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/List.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/ListMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/ListSet.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/LongMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/Map.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/NumericRange.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/Range.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/RedBlackTree.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/Seq.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/Set.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/SortedMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/SortedSet.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/StrictOptimizedSeqOps.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/TreeMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/TreeSet.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/TrieIterator.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/Vector.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/immutable/package.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/AnyRefMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/ArrayBuffer.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/BitSet.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/Buffer.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/Builder.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/FlatHashTable.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/Growable.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/GrowableBuilder.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/HashMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/HashSet.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/HashTable.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/ImmutableBuilder.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/Iterable.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/LinkedHashMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/LinkedHashSet.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/ListBuffer.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/LongMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/Map.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/RedBlackTree.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/ReusableBuilder.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/Seq.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/Set.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/Shrinkable.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/SortedMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/SortedSet.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/TreeMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/TreeSet.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/mutable/WeakHashMap.scala, /home/travis/build/scala/collection-strawman/collections/src/main/scala/strawman/collection/package.scala
java.lang.AssertionError: assertion failed: bad parameter reference this.T at tryCatchPatterns
the parameter is type T in class IntMapIterator but the prefix strawman.collection.immutable.IntMapIterator(this)
does not define any corresponding arguments.
	at dotty.DottyPredef$.assertFail(DottyPredef.scala:39)
	at dotty.tools.dotc.core.Types$NamedType.argDenot(Types.scala:1747)
	at dotty.tools.dotc.core.Types$NamedType.fromDesignator$1(Types.scala:1675)
	at dotty.tools.dotc.core.Types$NamedType.computeDenot(Types.scala:1696)
	at dotty.tools.dotc.core.Types$NamedType.denotAt(Types.scala:1647)
	at dotty.tools.dotc.core.Types$NamedType.denot(Types.scala:1636)
	at dotty.tools.dotc.core.Types$NamedType.info(Types.scala:1630)
	at dotty.tools.dotc.core.TypeComparer.secondTry(TypeComparer.scala:282)
	at dotty.tools.dotc.core.TypeComparer.firstTry(TypeComparer.scala:277)
	at dotty.tools.dotc.core.TypeComparer.op$7(TypeComparer.scala:118)
	at dotty.tools.dotc.core.TypeComparer.op$1(TypeComparer.scala:32)
	at dotty.tools.dotc.core.TypeComparer.isSubType(TypeComparer.scala:109)
	at dotty.tools.dotc.core.ConstraintHandling.isSubTypeWhenFrozen(ConstraintHandling.scala:161)
	at dotty.tools.dotc.core.Types$Type.op$17(Types.scala:771)
	at dotty.tools.dotc.core.Types$Type.frozen_$less$colon$less(Types.scala:770)
	at dotty.tools.dotc.core.Types$Type.overrides(Types.scala:807)
	at dotty.tools.dotc.typer.RefChecks$.compatibleTypes$4(RefChecks.scala:272)
	at dotty.tools.dotc.typer.RefChecks$.checkOverride$1(RefChecks.scala:380)
	at dotty.tools.dotc.typer.RefChecks$.dotty$tools$dotc$typer$RefChecks$$$checkAllOverrides(RefChecks.scala:401)
	at dotty.tools.dotc.typer.RefChecks.transformTemplate(RefChecks.scala:846)
	at dotty.tools.dotc.transform.MegaPhase.goTemplate(MegaPhase.scala:906)
	at dotty.tools.dotc.transform.MegaPhase.goTemplate(MegaPhase.scala:907)
	at dotty.tools.dotc.transform.MegaPhase.goTemplate(MegaPhase.scala:907)
	at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:318)
	at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:365)
	at dotty.tools.dotc.transform.MegaPhase.transformNamed$1(MegaPhase.scala:234)
	at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:364)
	at dotty.tools.dotc.transform.MegaPhase.transformStat$2(MegaPhase.scala:373)
	at dotty.tools.dotc.transform.MegaPhase.$anonfun$1(MegaPhase.scala:378)
	at scala.collection.immutable.List.mapConserve(List.scala:176)
	at dotty.tools.dotc.transform.MegaPhase.transformStats(MegaPhase.scala:378)
	at dotty.tools.dotc.transform.MegaPhase.mapPackage$1(MegaPhase.scala:334)
	at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:337)
	at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:365)
	at dotty.tools.dotc.transform.MegaPhase.transformStat$2(MegaPhase.scala:375)
	at dotty.tools.dotc.transform.MegaPhase.$anonfun$1(MegaPhase.scala:378)
	at scala.collection.immutable.List.mapConserve(List.scala:176)
	at dotty.tools.dotc.transform.MegaPhase.transformStats(MegaPhase.scala:378)
	at dotty.tools.dotc.transform.MegaPhase.mapPackage$1(MegaPhase.scala:334)
	at dotty.tools.dotc.transform.MegaPhase.transformUnnamed$1(MegaPhase.scala:337)
	at dotty.tools.dotc.transform.MegaPhase.transformTree(MegaPhase.scala:365)
	at dotty.tools.dotc.transform.MegaPhase.transformUnit(MegaPhase.scala:384)
	at dotty.tools.dotc.transform.MegaPhase.run(MegaPhase.scala:396)
	at dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:289)
	at scala.collection.immutable.List.map(List.scala:287)
	at dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:291)
	at dotty.tools.dotc.Run.op$4(Run.scala:125)
	at dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:123)
	at scala.compat.java8.JProcedure1.apply(JProcedure1.java:18)
	at scala.compat.java8.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.IndexedSeqOptimized.foreach(IndexedSeqOptimized.scala:32)
	at scala.collection.IndexedSeqOptimized.foreach$(IndexedSeqOptimized.scala:29)
	at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:191)
	at dotty.tools.dotc.Run.runPhases$2(Run.scala:136)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:141)
	at scala.compat.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
	at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:86)
	at dotty.tools.dotc.Run.compileUnits(Run.scala:143)
	at dotty.tools.dotc.Run.compileSources(Run.scala:94)
	at dotty.tools.dotc.Run.compile(Run.scala:78)
	at dotty.tools.dotc.Driver.doCompile(Driver.scala:29)
	at dotty.tools.dotc.Driver.process(Driver.scala:127)
	at xsbt.CachedCompilerImpl.run(CompilerInterface.scala:63)
	at xsbt.CachedCompilerImpl.run(CompilerInterface.scala:53)
	at xsbt.CompilerInterface.run(CompilerInterface.scala:37)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:107)
	at sbt.compiler.AnalyzingCompiler.compile(AnalyzingCompiler.scala:53)
	at sbt.compiler.AnalyzingCompiler.compile(AnalyzingCompiler.scala:47)
	at sbt.compiler.MixedAnalyzingCompiler$$anonfun$compileScala$1$1.apply$mcV$sp(MixedAnalyzingCompiler.scala:50)
	at sbt.compiler.MixedAnalyzingCompiler$$anonfun$compileScala$1$1.apply(MixedAnalyzingCompiler.scala:50)
	at sbt.compiler.MixedAnalyzingCompiler$$anonfun$compileScala$1$1.apply(MixedAnalyzingCompiler.scala:50)
	at sbt.compiler.MixedAnalyzingCompiler.timed(MixedAnalyzingCompiler.scala:74)
	at sbt.compiler.MixedAnalyzingCompiler.compileScala$1(MixedAnalyzingCompiler.scala:49)
	at sbt.compiler.MixedAnalyzingCompiler.compile(MixedAnalyzingCompiler.scala:64)
	at sbt.compiler.IC$$anonfun$compileInternal$1.apply(IncrementalCompiler.scala:160)
	at sbt.compiler.IC$$anonfun$compileInternal$1.apply(IncrementalCompiler.scala:160)
	at sbt.inc.IncrementalCompile$$anonfun$doCompile$1.apply(Compile.scala:66)
	at sbt.inc.IncrementalCompile$$anonfun$doCompile$1.apply(Compile.scala:64)
	at sbt.inc.IncrementalCommon.cycle(IncrementalCommon.scala:32)
	at sbt.inc.Incremental$$anonfun$1.apply(Incremental.scala:72)
	at sbt.inc.Incremental$$anonfun$1.apply(Incremental.scala:71)
	at sbt.inc.Incremental$.manageClassfiles(Incremental.scala:99)
	at sbt.inc.Incremental$.compile(Incremental.scala:71)
	at sbt.inc.IncrementalCompile$.apply(Compile.scala:54)
	at sbt.compiler.IC$.compileInternal(IncrementalCompiler.scala:160)
	at sbt.compiler.IC$.incrementalCompile(IncrementalCompiler.scala:138)
	at sbt.Compiler$.compile(Compiler.scala:155)
	at sbt.Compiler$.compile(Compiler.scala:141)
	at sbt.Defaults$.sbt$Defaults$$compileIncrementalTaskImpl(Defaults.scala:913)
	at sbt.Defaults$$anonfun$compileIncrementalTask$1.apply(Defaults.scala:904)
	at sbt.Defaults$$anonfun$compileIncrementalTask$1.apply(Defaults.scala:902)
	at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
	at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:40)
	at sbt.std.Transform$$anon$4.work(System.scala:63)
	at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:228)
	at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:228)
	at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:17)
	at sbt.Execute.work(Execute.scala:237)
	at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:228)
	at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:228)
	at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:159)
	at sbt.CompletionService$$anon$2.call(CompletionService.scala:28)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
[error] (collectionsJVM/compile:compileIncremental) java.lang.AssertionError: assertion failed: bad parameter reference this.T at tryCatchPatterns
[error] the parameter is type T in class IntMapIterator but the prefix strawman.collection.immutable.IntMapIterator(this)
[error] does not define any corresponding arguments.

@szeiger
Copy link
Contributor Author

szeiger commented Jan 8, 2018

Opened scala/scala3#3775 for this

@allanrenucci
Copy link
Contributor

You can make dotty happy by defining def IntMapIterator.next as def IntMapIterator.next(). Same for LongMap.

@szeiger
Copy link
Contributor Author

szeiger commented Jan 8, 2018

@allanrenucci Thanks, that does the trick.

@julienrf I suggest we look into possible further modularization after merging back into scala/scala. Right now I'd like to get these changes merged quickly so I can reduce the diff of my integration branches (which is thousands of loc for both collection-strawman and scala)

trait DefaultMap[K, +V] extends Map[K, V] { self =>

// Members declared in IterableOps
def iterableFactory: IterableFactoryLike[Iterable] = Iterable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could even refine the return type to IterableFactory[Iterable]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need IterableFactoryLike at all. The only use is in View where automatic buffering would be a sensible alternative with a simpler API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could try something like that, indeed.


def map[V2](f: ((Int, T)) => (Int, V2)): IntMap[V2] = intMapFromIterable(View.Map(toIterable, f))

def flatMap[V2](f: ((Int, T)) => IterableOnce[(Int, V2)]): IntMap[V2] = intMapFromIterable(View.FlatMap(toIterable, f))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of completeness you might also want to add an overload for concat. And you probably want to refine withFilter as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add concat. I was unable to implement withFilter without triggering the same compiler bug I ran into earlier (when WithFilter was still a trait, IIRC, which is what it has to be in order to get the types right in these cases). I'll try to get back to this at some point and remove the path-dependent WithFilter types.

with StrictOptimizedIterableOps[(Long, T), Iterable, LongMap[T]] {

protected[this] def fromSpecificIterable(coll: strawman.collection.Iterable[(Long, T)]): LongMap[T] = {
//TODO should this be the default implementation of this method in StrictOptimizedIterableOps?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, probably, that’s a good point. I’ve noticed that we could probably provide more default implementations of the factory methods in the Ops traits in general. I’ve created #335 to keep track of that.

b.addAll(coll)
b.result()
}
def iterableFactory: IterableFactoryLike[Iterable] = Iterable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would refine the return type to IterableFactory[Iterable], here too.

arm
}

/*override*/ def updated[V1 >: V](key: K, value: V1): AnyRefMap[K, V1] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed we don’t have an updated operation anymore on mutable Maps. We should probably deprecate that one, what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a good replacement? updated can widen the value type which otherwise requires a cast. The scalac codebase doesn't use the method though.

Copy link
Contributor

@julienrf julienrf Jan 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The replacement is myMutableMap.toMap.updated(k, v).to(AnyRefMap)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would never be a suitable replacement because it's horribly slow. If you didn't care about performance you wouldn't be using an AnyRefMap in the first place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've found a solution: I'll add AnyRefMap.from. The object can't implement any of the standard factories, so this method doesn't exist yet, but it's a natural place to perform a widening copy and we can easily optimize the case where you're copying an existing AnyRefMap. With this method m.updated[V1](k,v) becomes { val m2 = AnyRefMap.from[K, V1](m); m2 += ((k,v)); m2 }.

}

/** Adds a new key/value pair to this map and returns the map. */
def +=(key: Long, value: V): this.type = { update(key, value); this }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks weird, why don’t we take a tuple as parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid boxing and tupling. That's two unnecessary allocations per call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify further: This is an overload. LongMap inherits the tupled and boxed version from Growable.

@@ -53,6 +53,7 @@ trait Map[K, V]
trait MapOps[K, V, +CC[X, Y] <: MapOps[X, Y, CC, _], +C <: MapOps[K, V, CC, C]]
extends IterableOps[(K, V), Iterable, C]
with collection.MapOps[K, V, CC, C]
with Growable[(K, V)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@szeiger szeiger merged commit 38bb658 into master Jan 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants