From 0581e8bac449ac720bd666e7f7fdb010d810a3cd Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 29 Nov 2013 16:59:22 +0100 Subject: SimpleMap.foreachKey -> foreachBinding --- src/dotty/tools/dotc/core/Constraint.scala | 18 +++++++++--------- src/dotty/tools/dotc/core/TypeOps.scala | 8 ++++---- src/dotty/tools/dotc/typer/Inferencing.scala | 6 ++---- src/dotty/tools/dotc/util/SimpleMap.scala | 21 +++++++++------------ 4 files changed, 24 insertions(+), 29 deletions(-) (limited to 'src/dotty') diff --git a/src/dotty/tools/dotc/core/Constraint.scala b/src/dotty/tools/dotc/core/Constraint.scala index c32c9ce42..a1f52cad7 100644 --- a/src/dotty/tools/dotc/core/Constraint.scala +++ b/src/dotty/tools/dotc/core/Constraint.scala @@ -165,17 +165,17 @@ class Constraint(val myMap: SimpleMap[PolyType, Array[Type]]) extends AnyVal wit /** Perform operation `op` on all typevars, or only on uninstantiated * typevars, depending on whether `uninstOnly` is set or not. */ - def foreachTypeVar(op: TypeVar => Unit, uninstOnly: Boolean = false): Unit = myMap.foreachKey { poly => - val entries = myMap(poly) - for (i <- 0 until paramCount(entries)) { - def qualifies(tv: TypeVar) = - if (uninstOnly) isBounds(entries(i)) else !tv.inst.exists - typeVar(entries, i) match { - case tv: TypeVar if qualifies(tv) => op(tv) - case _ => + def foreachTypeVar(op: TypeVar => Unit, uninstOnly: Boolean = false): Unit = + myMap.foreachBinding { (poly, entries) => + for (i <- 0 until paramCount(entries)) { + def qualifies(tv: TypeVar) = + if (uninstOnly) isBounds(entries(i)) else !tv.inst.exists + typeVar(entries, i) match { + case tv: TypeVar if qualifies(tv) => op(tv) + case _ => + } } } - } /** Perform operation `op` on all uninstantiated typevars. */ diff --git a/src/dotty/tools/dotc/core/TypeOps.scala b/src/dotty/tools/dotc/core/TypeOps.scala index 4c4e8face..d9e5d7f96 100644 --- a/src/dotty/tools/dotc/core/TypeOps.scala +++ b/src/dotty/tools/dotc/core/TypeOps.scala @@ -154,15 +154,15 @@ trait TypeOps { this: Context => throw new TypeError(s"unexpected parent type: $tp") } val parentRefs = parents map normalizeToRef - refinements foreachKey { name => + refinements foreachBinding { (name, refinedInfo) => assert(decls.lookup(name) == NoSymbol, // DEBUG s"redefinition of ${decls.lookup(name).debugString} in ${cls.showLocated}") - enterArgBinding(formals(name), refinements(name)) + enterArgBinding(formals(name), refinedInfo) } // These two loops cannot be fused because second loop assumes that // all arguments have been entered in `decls`. - refinements foreachKey { name => - forwardRefs(formals(name), refinements(name), parentRefs) + refinements foreachBinding { (name, refinedInfo) => + forwardRefs(formals(name), refinedInfo, parentRefs) } parentRefs } diff --git a/src/dotty/tools/dotc/typer/Inferencing.scala b/src/dotty/tools/dotc/typer/Inferencing.scala index 737903197..e49769d9a 100644 --- a/src/dotty/tools/dotc/typer/Inferencing.scala +++ b/src/dotty/tools/dotc/typer/Inferencing.scala @@ -294,8 +294,7 @@ object Inferencing { val vs = tp.variances(tvar => (constraint contains tvar) && qualifies(tvar)) println(s"variances = $vs") var changed = false - vs foreachKey { tvar => - val v = vs(tvar) + vs foreachBinding { (tvar, v) => if (v != 0) { println(s"interpolate ${if (v == 1) "co" else "contra"}variant ${tvar.show} in ${tp.show}") tvar.instantiate(fromBelow = v == 1) @@ -321,8 +320,7 @@ object Inferencing { val constraint = ctx.typerState.constraint val vs = tp.variances(constraint contains _) var result: Option[TypeVar] = None - vs foreachKey { tvar => - val v = vs(tvar) + vs foreachBinding { (tvar, v) => if (v == 1) tvar.instantiate(fromBelow = false) else if (v == -1) tvar.instantiate(fromBelow = true) else { diff --git a/src/dotty/tools/dotc/util/SimpleMap.scala b/src/dotty/tools/dotc/util/SimpleMap.scala index c31540c8c..df425890a 100644 --- a/src/dotty/tools/dotc/util/SimpleMap.scala +++ b/src/dotty/tools/dotc/util/SimpleMap.scala @@ -9,13 +9,10 @@ abstract class SimpleMap[K <: AnyRef, +V >: Null <: AnyRef] extends (K => V) { def updated[V1 >: V <: AnyRef](k: K, v: V1): SimpleMap[K, V1] def contains(k: K): Boolean = apply(k) != null def mapValues[V1 >: V <: AnyRef](f: V1 => V1): SimpleMap[K, V1] - def foreachKey(f: K => Unit): Unit + def foreachBinding(f: (K, V) => Unit): Unit def map2[T](f: (K, V) => T): List[T] = { val buf = new ListBuffer[T] - foreachKey { key => - val k = key.asInstanceOf[K] - buf += f(k, this(k)) - } + foreachBinding((k, v) => buf += f(k, v)) buf.toList } def keys: List[K] = map2((k, v) => k) @@ -36,7 +33,7 @@ object SimpleMap { def remove(k: AnyRef) = this def updated[V1 >: Null <: AnyRef](k: AnyRef, v: V1) = new Map1(k, v) def mapValues[V1 >: Null <: AnyRef](f: V1 => V1) = this - def foreachKey(f: AnyRef => Unit) = () + def foreachBinding(f: (AnyRef, Null) => Unit) = () } def Empty[K <: AnyRef] = myEmpty.asInstanceOf[SimpleMap[K, Null]] @@ -56,7 +53,7 @@ object SimpleMap { val w1 = f(v1) if (v1 eq w1) this else new Map1(k1, w1) } - def foreachKey(f: K => Unit) = f(k1) + def foreachBinding(f: (K, V) => Unit) = f(k1, v1) } class Map2[K <: AnyRef, +V >: Null <: AnyRef] (k1: K, v1: V, k2: K, v2: V) extends SimpleMap[K, V] { @@ -78,7 +75,7 @@ object SimpleMap { if ((v1 eq w1) && (v2 eq w2)) this else new Map2(k1, w1, k2, w2) } - def foreachKey(f: K => Unit) = { f(k1); f(k2) } + def foreachBinding(f: (K, V) => Unit) = { f(k1, v1); f(k2, v2) } } class Map3[K <: AnyRef, +V >: Null <: AnyRef] (k1: K, v1: V, k2: K, v2: V, k3: K, v3: V) extends SimpleMap[K, V] { @@ -103,7 +100,7 @@ object SimpleMap { if ((v1 eq w1) && (v2 eq w2) && (v3 eq w3)) this else new Map3(k1, w1, k2, w2, k3, w3) } - def foreachKey(f: K => Unit) = { f(k1); f(k2); f(k3) } + def foreachBinding(f: (K, V) => Unit) = { f(k1, v1); f(k2, v2); f(k3, v3) } } class Map4[K <: AnyRef, +V >: Null <: AnyRef] (k1: K, v1: V, k2: K, v2: V, k3: K, v3: V, k4: K, v4: V) extends SimpleMap[K, V] { @@ -131,7 +128,7 @@ object SimpleMap { if ((v1 eq w1) && (v2 eq w2) && (v3 eq w3) && (v4 eq w4)) this else new Map4(k1, w1, k2, w2, k3, w3, k4, w4) } - def foreachKey(f: K => Unit) = { f(k1); f(k2); f(k3); f(k4) } + def foreachBinding(f: (K, V) => Unit) = { f(k1, v1); f(k2, v2); f(k3, v3); f(k4, v4) } } class MapMore[K <: AnyRef, +V >: Null <: AnyRef](bindings: Array[AnyRef]) extends SimpleMap[K, V] { @@ -215,10 +212,10 @@ object SimpleMap { if (bindings1 eq bindings) this else new MapMore(bindings1) } - def foreachKey(f: K => Unit) = { + def foreachBinding(f: (K, V) => Unit) = { var i = 0 while (i < bindings.length) { - f(key(i)) + f(key(i), value(i)) i += 2 } } -- cgit v1.2.3