aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/core/Types.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2014-08-03 20:51:12 +0200
committerMartin Odersky <odersky@gmail.com>2014-08-03 21:03:51 +0200
commit85044e451ea99ef49fe2348148bdd4296b1db595 (patch)
tree9af58299290470d8938daa8ca76ff06001ca577c /src/dotty/tools/dotc/core/Types.scala
parente25232d5b4fdc1ae7bc5a44ad06e31a00699dbaf (diff)
downloaddotty-85044e451ea99ef49fe2348148bdd4296b1db595.tar.gz
dotty-85044e451ea99ef49fe2348148bdd4296b1db595.tar.bz2
dotty-85044e451ea99ef49fe2348148bdd4296b1db595.zip
Type#foreachPart
Added method to traverse all parts of a type.
Diffstat (limited to 'src/dotty/tools/dotc/core/Types.scala')
-rw-r--r--src/dotty/tools/dotc/core/Types.scala12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/dotty/tools/dotc/core/Types.scala b/src/dotty/tools/dotc/core/Types.scala
index 474958b86..592b7b55f 100644
--- a/src/dotty/tools/dotc/core/Types.scala
+++ b/src/dotty/tools/dotc/core/Types.scala
@@ -208,6 +208,10 @@ object Types {
final def forallParts(p: Type => Boolean)(implicit ctx: Context): Boolean =
!existsPart(!p(_))
+ /** Performs operation on all parts of this type */
+ final def foreachPart(p: Type => Unit)(implicit ctx: Context): Unit =
+ new ForeachAccumulator(p).apply((), this)
+
/** The parts of this type which are type or term refs */
final def namedParts(implicit ctx: Context): collection.Set[NamedType] =
namedPartsWith(alwaysTrue)
@@ -218,9 +222,6 @@ object Types {
def namedPartsWith(p: NamedType => Boolean)(implicit ctx: Context): collection.Set[NamedType] =
new NamedPartsAccumulator(p).apply(mutable.LinkedHashSet(), this)
- // needed?
- //final def foreach(f: Type => Unit): Unit = ???
-
/** Map function `f` over elements of an AndType, rebuilding with function `g` */
def mapReduceAnd[T](f: Type => T)(g: (T, T) => T)(implicit ctx: Context): T = stripTypeVar match {
case AndType(tp1, tp2) => g(tp1.mapReduceAnd(f)(g), tp2.mapReduceAnd(f)(g))
@@ -2571,6 +2572,11 @@ object Types {
def apply(x: Boolean, tp: Type) = x || p(tp) || foldOver(x, tp)
}
+ class ForeachAccumulator(p: Type => Unit)(implicit ctx: Context) extends TypeAccumulator[Unit] {
+ override def stopAtStatic = false
+ def apply(x: Unit, tp: Type): Unit = foldOver(p(tp), tp)
+ }
+
class NamedPartsAccumulator(p: NamedType => Boolean)(implicit ctx: Context) extends TypeAccumulator[mutable.Set[NamedType]] {
override def stopAtStatic = false
def maybeAdd(x: mutable.Set[NamedType], tp: NamedType) = if (p(tp)) x += tp else x