Skip to content

Commit 85044e4

Browse files
committed
Type#foreachPart
Added method to traverse all parts of a type.
1 parent e25232d commit 85044e4

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/dotty/tools/dotc/core/Types.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ object Types {
208208
final def forallParts(p: Type => Boolean)(implicit ctx: Context): Boolean =
209209
!existsPart(!p(_))
210210

211+
/** Performs operation on all parts of this type */
212+
final def foreachPart(p: Type => Unit)(implicit ctx: Context): Unit =
213+
new ForeachAccumulator(p).apply((), this)
214+
211215
/** The parts of this type which are type or term refs */
212216
final def namedParts(implicit ctx: Context): collection.Set[NamedType] =
213217
namedPartsWith(alwaysTrue)
@@ -218,9 +222,6 @@ object Types {
218222
def namedPartsWith(p: NamedType => Boolean)(implicit ctx: Context): collection.Set[NamedType] =
219223
new NamedPartsAccumulator(p).apply(mutable.LinkedHashSet(), this)
220224

221-
// needed?
222-
//final def foreach(f: Type => Unit): Unit = ???
223-
224225
/** Map function `f` over elements of an AndType, rebuilding with function `g` */
225226
def mapReduceAnd[T](f: Type => T)(g: (T, T) => T)(implicit ctx: Context): T = stripTypeVar match {
226227
case AndType(tp1, tp2) => g(tp1.mapReduceAnd(f)(g), tp2.mapReduceAnd(f)(g))
@@ -2571,6 +2572,11 @@ object Types {
25712572
def apply(x: Boolean, tp: Type) = x || p(tp) || foldOver(x, tp)
25722573
}
25732574

2575+
class ForeachAccumulator(p: Type => Unit)(implicit ctx: Context) extends TypeAccumulator[Unit] {
2576+
override def stopAtStatic = false
2577+
def apply(x: Unit, tp: Type): Unit = foldOver(p(tp), tp)
2578+
}
2579+
25742580
class NamedPartsAccumulator(p: NamedType => Boolean)(implicit ctx: Context) extends TypeAccumulator[mutable.Set[NamedType]] {
25752581
override def stopAtStatic = false
25762582
def maybeAdd(x: mutable.Set[NamedType], tp: NamedType) = if (p(tp)) x += tp else x

0 commit comments

Comments
 (0)