diff options
author | Martin Odersky <odersky@gmail.com> | 2013-10-02 14:03:45 +0200 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2013-10-02 14:03:45 +0200 |
commit | fda5cdccad52657191e756ad2d47595a9c25b0ff (patch) | |
tree | a1c385a93b50097c7645aca8666d7bfd4b69a014 | |
parent | f039fa7fa2544998426764bd05ea8f18179eb0bd (diff) | |
download | dotty-fda5cdccad52657191e756ad2d47595a9c25b0ff.tar.gz dotty-fda5cdccad52657191e756ad2d47595a9c25b0ff.tar.bz2 dotty-fda5cdccad52657191e756ad2d47595a9c25b0ff.zip |
Introducing mapReduce for OrTypes and AndTypes
-rw-r--r-- | src/dotty/tools/dotc/core/TypeComparer.scala | 8 | ||||
-rw-r--r-- | src/dotty/tools/dotc/core/Types.scala | 14 |
2 files changed, 17 insertions, 5 deletions
diff --git a/src/dotty/tools/dotc/core/TypeComparer.scala b/src/dotty/tools/dotc/core/TypeComparer.scala index 697526c23..14b0413ec 100644 --- a/src/dotty/tools/dotc/core/TypeComparer.scala +++ b/src/dotty/tools/dotc/core/TypeComparer.scala @@ -520,7 +520,7 @@ class TypeComparer(initctx: Context) extends DotClass { * Such TypeBounds can also be arbitrarily instantiated. In both cases we need to * make sure that such types do not actually arise in source programs. */ - final def andType(tp1: Type, tp2: Type) = { + final def andType(tp1: Type, tp2: Type) = ctx.traceIndented(s"glb(${tp1.show}, ${tp2.show})", show = true) { val t1 = distributeAnd(tp1, tp2) if (t1.exists) t1 else { @@ -729,7 +729,13 @@ class TypeComparer(initctx: Context) extends DotClass { case _ => false } +/* + def widenInferred(tp: Type) = tp match { + case tp: OrType => + val alts = tp.mapReduceOr(_ :: Nil)(_ ::: _) + } +*/ def copyIn(ctx: Context) = new TypeComparer(ctx) } diff --git a/src/dotty/tools/dotc/core/Types.scala b/src/dotty/tools/dotc/core/Types.scala index d8145e13c..978c39cc9 100644 --- a/src/dotty/tools/dotc/core/Types.scala +++ b/src/dotty/tools/dotc/core/Types.scala @@ -193,14 +193,20 @@ object Types { final def foreach(f: Type => Unit): Unit = ??? /** Map function over elements of an AndType, rebuilding with & */ - def mapAnd(f: Type => Type)(implicit ctx: Context): Type = stripTypeVar match { - case AndType(tp1, tp2) => tp1.mapAnd(f) & tp2.mapAnd(f) + def mapAnd(f: Type => Type)(implicit ctx: Context): Type = + mapReduceAnd(f)(_ & _) + + 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)) case _ => f(this) } /** Map function over elements of an OrType, rebuilding with | */ - final def mapOr(f: Type => Type)(implicit ctx: Context): Type = stripTypeVar match { - case OrType(tp1, tp2) => tp1.mapOr(f) | tp2.mapOr(f) + final def mapOr(f: Type => Type)(implicit ctx: Context): Type = + mapReduceOr(f)(_ | _) + + final def mapReduceOr[T](f: Type => T)(g: (T, T) => T)(implicit ctx: Context): T = stripTypeVar match { + case OrType(tp1, tp2) => g(tp1.mapReduceOr(f)(g), tp2.mapReduceOr(f)(g)) case _ => f(this) } |